<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>jQuery | 李世龙</title><meta name="keywords" content="jQuery"><meta name="author" content="lee"><meta name="copyright" content="lee"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="jQuery的基本使用">
<meta property="og:type" content="article">
<meta property="og:title" content="jQuery">
<meta property="og:url" content="http://blog.cai2.wang/2021/11/20/Done/jQuery/index.html">
<meta property="og:site_name" content="李世龙">
<meta property="og:description" content="jQuery的基本使用">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/Cai2w/cdn/img/undraw_social_girl_562b.png">
<meta property="article:published_time" content="2021-11-20T02:29:35.000Z">
<meta property="article:modified_time" content="2022-08-23T11:33:18.372Z">
<meta property="article:author" content="lee">
<meta property="article:tag" content="jQuery">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://cdn.jsdelivr.net/gh/Cai2w/cdn/img/undraw_social_girl_562b.png"><link rel="shortcut icon" href="https://cdn.jsdelivr.net/gh/Cai2w/cdn/img/avatar.jpg"><link rel="canonical" href="http://blog.cai2.wang/2021/11/20/Done/jQuery/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: {"path":"search.xml","languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: {"defaultEncoding":2,"translateDelay":0,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"简"},
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":200},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '天',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: {"chs_to_cht":"你已切换为繁体","cht_to_chs":"你已切换为简体","day_to_night":"你已切换为深色模式","night_to_day":"你已切换为浅色模式","bgLight":"#49b1f5","bgDark":"#121212","position":"bottom-left"},
  source: {
    jQuery: 'https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js',
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/js/jquery.justifiedGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/css/justifiedGallery.min.css'
    },
    fancybox: {
      js: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js',
      css: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.css'
    }
  },
  isPhotoFigcaption: true,
  islazyload: true,
  isanchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'jQuery',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2022-08-23 19:33:18'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const fontSizeVal = saveToLocal.get('global-font-size')
    if (fontSizeVal !== undefined) {
      document.documentElement.style.setProperty('--global-font-size', fontSizeVal + 'px')
    }
    })(window)</script><link rel="stylesheet" href="/css/iconfont.css"><link rel="stylesheet" href="/css/index.min.css"><link rel="stylesheet" href="/css/bg.css"><meta name="generator" content="Hexo 5.4.0"></head><body><div id="loading-box"><div class="loading-left-bg"></div><div class="loading-right-bg"></div><div class="spinner-box"><div class="configure-border-1"><div class="configure-core"></div></div><div class="configure-border-2"><div class="configure-core"></div></div><div class="loading-word">加载中...</div></div></div><div id="web_bg"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="https://cdn.jsdelivr.net/gh/Cai2w/cdn/img/avatar.jpg" onerror="onerror=null;src='https://cdn.jsdelivr.net/gh/Cai2w/cdn/img/friend_404.gif'" alt="avatar"/></div><div class="site-data"><div class="data-item is-center"><div class="data-item-link"><a href="/archives/"><div class="headline">文章</div><div class="length-num">13</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/tags/"><div class="headline">标签</div><div class="length-num">9</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/categories/"><div class="headline">分类</div><div class="length-num">8</div></a></div></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 娱乐</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/video/"><i class="fa-fw fas fa-video"></i><span> 视频</span></a></li><li><a class="site-page child" href="/love/"><i class="fa-fw fa fa-heartbeat"></i><span> 心动</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/my/"><i class="fa-fw fas fa-ad"></i><span> 我的</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-seedling"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="not-top-img" id="page-header"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">李世龙</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 娱乐</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/video/"><i class="fa-fw fas fa-video"></i><span> 视频</span></a></li><li><a class="site-page child" href="/love/"><i class="fa-fw fa fa-heartbeat"></i><span> 心动</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/my/"><i class="fa-fw fas fa-ad"></i><span> 我的</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-seedling"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav></header><main class="layout" id="content-inner"><div id="post"><div id="post-info"><h1 class="post-title">jQuery</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2021-11-20T02:29:35.000Z" title="发表于 2021-11-20 10:29:35">2021-11-20</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2022-08-23T11:33:18.372Z" title="更新于 2022-08-23 19:33:18">2022-08-23</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/jQuery/">jQuery</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">10.3k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>45分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="jQuery"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div><article class="post-content" id="article-container"><h1 id="1-jQuery-介绍"><a href="#1-jQuery-介绍" class="headerlink" title="1, jQuery 介绍"></a>1, jQuery 介绍</h1><h2 id="1-1-jQuery概述"><a href="#1-1-jQuery概述" class="headerlink" title="1,1 jQuery概述"></a>1,1 jQuery概述</h2><p>jQuery是一个快速、简洁的JavaScript框架 , jQuery设计的宗旨是“write Less，Do More”</p>
<p>即倡导写更少的代码，做更多的事情。它封装JavaScript常用的功能代码 , 提供了一套易于使用的 API 可以跨浏览</p>
<p>器工作 , 使 HTML 文档的遍历和操作 , 事件的处理 , 动画的设计和 Ajax 交互更加简介</p>
<h2 id="1-2-jQuery框架的下载与引用"><a href="#1-2-jQuery框架的下载与引用" class="headerlink" title="1,2 jQuery框架的下载与引用"></a>1,2 jQuery框架的下载与引用</h2><p><strong>jQuery的官网:<a target="_blank" rel="noopener" href="https://jquery.com/">https://jquery.com/</a></strong></p>
<p><img src= "https://cdn.jsdelivr.net/gh/Cai2w/cdn/img/loading.gif" data-lazy-src="D:\blog\source_posts\04-jQuery.assets\image-20211110150509281.png" alt="image-20211110150509281"></p>
<p><img src= "https://cdn.jsdelivr.net/gh/Cai2w/cdn/img/loading.gif" data-lazy-src="D:\blog\source_posts\04-jQuery.assets\image-20211110150610324.png" alt="image-20211110150610324"></p>
<h2 id="1-3-jQuery的版本"><a href="#1-3-jQuery的版本" class="headerlink" title="1,3 jQuery的版本"></a>1,3 jQuery的版本</h2><ul>
<li>1.x : 兼容IE6,7,8 , 使用最为广泛的 , 官方只做 BUG 维护 , 功能不会更新了 , 对于一般的项目来说使用该版本即可, 最终版 : 1.12.4</li>
<li>2.x : 兼容IE6,7,8 , 使用最为广泛的 , 官方只做 BUG 维护 , 功能不会更新了 , 如果考虑不兼容低版本的浏览器可以使用 , 最终版 : 2.2.4</li>
<li>3.x : 不兼容IE6,7,8 , 只支持最新的浏览器 , 除非特殊需要 , 一般很少使用3.x版本</li>
<li>**开发版和生产版 : **命名为 <strong>jQuery-x-x-x.js</strong> 为开发版 , 命名为 <strong>jQuery-x-x-x.min.js</strong> 为生产版 , 开发版的源码格式良好 , 有代码注释 , 和锁紧的格式 , 方便开发人员查看源码 , 但是体积比较大 , 生产办没有代码的缩进和注释 , 而且去掉了换行和空格,不方便查看,但是体量小</li>
</ul>
<h2 id="1-4-jQuery引入"><a href="#1-4-jQuery引入" class="headerlink" title="1,4 jQuery引入"></a>1,4 jQuery引入</h2><p>在需要使用 jQuery 的 html 中使用 js 的外部引入方式引入</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- </span></span><br><span class="line"><span class="comment">    jQuery引入分为两个步骤</span></span><br><span class="line"><span class="comment">    1,将外部的jQuery代码复制到当前的项目中</span></span><br><span class="line"><span class="comment">    2,在html页面中使用script标签引入外部的jQuery文件</span></span><br><span class="line"><span class="comment">--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">type</span>=<span class="string">&quot;text/javascript&quot;</span> <span class="attr">src</span>=<span class="string">&quot;js/jquery-3.6.0.min.js&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>



<blockquote>
<p>引入成功的测试</p>
</blockquote>
<p>在 jQuery 有一个核心函数 <strong>jQuery , 可以简化成 $</strong></p>
<p>调用 jQuery 的时候需要传递一个匿名函数</p>
<p>当页面加载完毕之后 , 就会执行这个函数</p>
<p>相当于页面加载事件 <strong>window.onload = function(){}</strong></p>
<p><strong>语法:</strong></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">jQuery(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">   <span class="comment">// 页面加载事件 </span></span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line">$(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">   <span class="comment">// 页面加载事件 </span></span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>

<ul>
<li>测试代码1</li>
</ul>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">type</span>=<span class="string">&quot;text/javascript&quot;</span>&gt;</span><span class="javascript"></span></span><br><span class="line"><span class="javascript">    jQuery(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">        alert(<span class="string">&#x27;Hello,jQuery!!!&#x27;</span>);</span></span><br><span class="line"><span class="javascript">    &#125;);</span></span><br><span class="line"><span class="javascript"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>

<img src= "https://cdn.jsdelivr.net/gh/Cai2w/cdn/img/loading.gif" data-lazy-src="D:\blog\source\_posts\04-jQuery.assets\image-20211110152839513.png" alt="image-20211110152839513" style="zoom: 80%;border:solid" />    



<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">type</span>=<span class="string">&quot;text/javascript&quot;</span>&gt;</span><span class="javascript"></span></span><br><span class="line"><span class="javascript">    $(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">        alert(<span class="string">&#x27;Hello,jQuery!!!&#x27;</span>);</span></span><br><span class="line"><span class="javascript">    &#125;);</span></span><br><span class="line"><span class="javascript"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>

<img src= "https://cdn.jsdelivr.net/gh/Cai2w/cdn/img/loading.gif" data-lazy-src="D:\blog\source\_posts\04-jQuery.assets\image-20211110152839513.png" alt="image-20211110152839513" style="zoom:80%;border:solid" />    



<p><strong>==jQuery的代码风格 : 支持操作的连缀写法 (链式编程)==</strong></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 改变第2个和第3个li的样式</span></span><br><span class="line"><span class="comment">// 第2个改成红色,第三个改成蓝色</span></span><br><span class="line">$(<span class="string">&#x27;ul li:eq(1)&#x27;</span>).css(<span class="string">&#x27;color&#x27;</span>,<span class="string">&#x27;red&#x27;</span>)</span><br><span class="line">.next().css(<span class="string">&#x27;color&#x27;</span>,<span class="string">&#x27;blue&#x27;</span>);</span><br></pre></td></tr></table></figure>





<h2 id="1-5-jQuery页面加载事件和JS页面加载事件的区别"><a href="#1-5-jQuery页面加载事件和JS页面加载事件的区别" class="headerlink" title="1,5 jQuery页面加载事件和JS页面加载事件的区别"></a>1,5 jQuery页面加载事件和JS页面加载事件的区别</h2><ul>
<li><strong>JS页面加载事件</strong></li>
</ul>
<p><strong>特点 : 只能写一个 , 绑定的函数多了 , 后边的会覆盖前面 , 等到页面中所有的资源加载完成之后再去执行事件中回调函数</strong></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// window被省略掉了</span></span><br><span class="line">onload = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    alert(<span class="string">&#x27;aaaaa&#x27;</span>);</span><br><span class="line">&#125;</span><br><span class="line">onload = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    alert(<span class="string">&#x27;bbbbb&#x27;</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><img src= "https://cdn.jsdelivr.net/gh/Cai2w/cdn/img/loading.gif" data-lazy-src="D:\blog\source_posts\04-jQuery.assets\image-20211110155413175.png" alt="image-20211110155413175"></p>
<ul>
<li><strong>jQuery中的页面加载事件</strong></li>
</ul>
<p><strong>可以编写多个 , 都会被执行 , 网页中所有的标签加载完毕之后 , 不管标签中内容是否加载完成都会执行</strong></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">$(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    alert(<span class="string">&#x27;aaaaa&#x27;</span>);</span><br><span class="line">&#125;);</span><br><span class="line">$(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    alert(<span class="string">&#x27;bbbbb&#x27;</span>);</span><br><span class="line">&#125;);</span><br><span class="line"><span class="comment">// 另外两种写法</span></span><br><span class="line">$(<span class="built_in">document</span>).ready(<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    </span><br><span class="line">&#125;);</span><br><span class="line">$().ready(<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    </span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>

<p><img src= "https://cdn.jsdelivr.net/gh/Cai2w/cdn/img/loading.gif" data-lazy-src="D:\blog\source_posts\04-jQuery.assets\image-20211110155712231.png" alt="image-20211110155712231"></p>
<p><img src= "https://cdn.jsdelivr.net/gh/Cai2w/cdn/img/loading.gif" data-lazy-src="D:\blog\source_posts\04-jQuery.assets\image-20211110155719309.png" alt="image-20211110155719309"></p>
<h2 id="1-6-jQuery对象和js对象之间的转换"><a href="#1-6-jQuery对象和js对象之间的转换" class="headerlink" title="1,6 jQuery对象和js对象之间的转换"></a>1,6 jQuery对象和js对象之间的转换</h2><p>jQuery本质虽然是 JS , 如果我们要使用 jQuery 的属性和方法来必须保证对象是一个 jQuery 对象 , 而不是 js 方式获得的 DOM 对象 , 二者的 API 不能混合使用 , 如果想要使用对方的 API 需要进行转换</p>
<p>jQuery 的本质是一个数组 , 数组中包含了多个 DOM 对象(document对象)</p>
<p><strong>==var jQuery = [DOM对象,DOM对象….]==</strong></p>
<p>两者之间的转换关系 : </p>
<ul>
<li>js 的 DOM 对象转换成 jQuery 对象 , <strong>穿马甲</strong> , 语法 : <strong>$(js的DOM对象)</strong></li>
<li>jQuery对象转换成 JS 对象 , <strong>脱马甲</strong> , 语法 : <strong>jQuery对象[索引] 或者 jQuery对象.get(索引)</strong></li>
</ul>
<blockquote>
<p>代码实例</p>
</blockquote>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">type</span>=<span class="string">&quot;text/javascript&quot;</span>&gt;</span><span class="javascript"></span></span><br><span class="line"><span class="javascript">    <span class="comment">// 使用jQuery的页面加载事件</span></span></span><br><span class="line"><span class="javascript">    $(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">        <span class="comment">// 使用JS的方式获取第一个div的文本内容</span></span></span><br><span class="line"><span class="javascript">        <span class="keyword">var</span> first = <span class="built_in">document</span>.getElementById(<span class="string">&#x27;first&#x27;</span>);</span></span><br><span class="line"><span class="javascript">        <span class="built_in">console</span>.log(first.innerHTML);</span></span><br><span class="line"><span class="javascript"></span></span><br><span class="line"><span class="javascript">        <span class="comment">/* </span></span></span><br><span class="line"><span class="comment"><span class="javascript">		使用jQuery对象获取第二个div的内容</span></span></span><br><span class="line"><span class="comment"><span class="javascript">		在jQuery对象中把设置标签体和获取标签题封装为一个函数,函数名html</span></span></span><br><span class="line"><span class="comment"><span class="javascript">		获取标签体:jQuery对象.html();</span></span></span><br><span class="line"><span class="comment"><span class="javascript">		设置标签体:jQuery对象.html(新的标签体);</span></span></span><br><span class="line"><span class="comment"><span class="javascript">		注意: jQuery对象变量名称前要加一个$,为了区分jQuery对象和JS对象</span></span></span><br><span class="line"><span class="comment"><span class="javascript">		*/</span></span></span><br><span class="line"><span class="javascript">        <span class="keyword">var</span> $second = $(<span class="string">&#x27;#second&#x27;</span>);</span></span><br><span class="line"><span class="javascript">        <span class="built_in">console</span>.log($second.html());</span></span><br><span class="line"><span class="javascript"></span></span><br><span class="line"><span class="javascript">        <span class="comment">// 将DOM对象转换为jQuery对象</span></span></span><br><span class="line"><span class="javascript">        <span class="keyword">var</span> $div1 = $(first);</span></span><br><span class="line"><span class="javascript">        $div1.html(<span class="string">&#x27;&lt;b&gt;我是jQ对象替换的内容&lt;/b&gt;&#x27;</span>);</span></span><br><span class="line"><span class="javascript"></span></span><br><span class="line"><span class="javascript">        <span class="comment">// 将jQuery对象转换为 DOM 对象</span></span></span><br><span class="line"><span class="javascript">        <span class="keyword">var</span> div2 = $second[<span class="number">0</span>];</span></span><br><span class="line"><span class="javascript">        div2.innerHTML = <span class="string">&#x27;&lt;b&gt;我是DOM对象替换的内容&lt;/b&gt;&#x27;</span></span></span><br><span class="line"><span class="javascript"></span></span><br><span class="line"><span class="javascript">        <span class="keyword">var</span> divSe = $second.get(<span class="number">0</span>);</span></span><br><span class="line"><span class="javascript">        <span class="built_in">console</span>.log(divSe.innerHTML);</span></span><br><span class="line"><span class="javascript">    &#125;);</span></span><br><span class="line"><span class="javascript"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">&quot;first&quot;</span>&gt;</span>我是第一个div<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">&quot;second&quot;</span>&gt;</span>我是第二个div<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br></pre></td></tr></table></figure>

<img src= "https://cdn.jsdelivr.net/gh/Cai2w/cdn/img/loading.gif" data-lazy-src="D:\blog\source\_posts\04-jQuery.assets\image-20211110162021783.png" alt="image-20211110162021783" style="zoom:80%;border: solid" />



<h1 id="2-jQuery-选择器"><a href="#2-jQuery-选择器" class="headerlink" title="2, jQuery 选择器"></a>2, jQuery 选择器</h1><h2 id="2-1-基本选择器"><a href="#2-1-基本选择器" class="headerlink" title="2,1 基本选择器"></a>2,1 基本选择器</h2><ul>
<li><p>标签选择器 : </p>
<ul>
<li><p><strong>$(‘表签名’)</strong></p>
</li>
<li><p>获取所有匹配表签名的元素</p>
</li>
</ul>
</li>
<li><p>id 选择器 :</p>
<ul>
<li><strong>$(‘#id属性值’)</strong></li>
<li>获取所有对应id属性值的元素</li>
</ul>
</li>
<li><p>类选择器 :</p>
<ul>
<li><strong>$(‘class属性值’)</strong></li>
<li>获取所有对应class属性值的元素</li>
</ul>
</li>
</ul>
<blockquote>
<p>代码实例 : <strong>id选择器</strong></p>
</blockquote>
<p>点击元素实现背景颜色的改变</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">&quot;one&quot;</span>&gt;</span>我是id为one的div元素<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>样式</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">style</span> <span class="attr">type</span>=<span class="string">&quot;text/css&quot;</span>&gt;</span><span class="css"></span></span><br><span class="line"><span class="css">    <span class="selector-id">#one</span> &#123;</span></span><br><span class="line"><span class="css">        <span class="attribute">width</span>: <span class="number">300px</span>;</span></span><br><span class="line"><span class="css">        <span class="attribute">height</span>: <span class="number">300px</span>;</span></span><br><span class="line"><span class="css">        <span class="attribute">background-color</span>: <span class="number">#0000FF</span>;</span></span><br><span class="line"><span class="css">        <span class="attribute">border</span>: solid black;</span></span><br><span class="line"><span class="css">    &#125;</span></span><br><span class="line"><span class="css"></span><span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br></pre></td></tr></table></figure>

<img src= "https://cdn.jsdelivr.net/gh/Cai2w/cdn/img/loading.gif" data-lazy-src="D:\blog\source\_posts\04-jQuery.assets\image-20211110164718396.png" alt="image-20211110164718396" style="zoom:33%;" />    

<p>js 实现</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 通过js实现点击元素变色</span></span><br><span class="line"><span class="keyword">var</span> div = <span class="built_in">document</span>.querySelector(<span class="string">&#x27;#one&#x27;</span>);</span><br><span class="line">div.addEventListener(<span class="string">&#x27;click&#x27;</span>,<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="built_in">this</span>.style.backgroundColor = <span class="string">&#x27;red&#x27;</span>;</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>

<p>jQuery实现</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 通过jQuery实现元素的点击变色</span></span><br><span class="line"><span class="keyword">var</span> $div = $(<span class="string">&#x27;#one&#x27;</span>);</span><br><span class="line"><span class="comment">/* </span></span><br><span class="line"><span class="comment">给元素添加一个鼠标点击的事件</span></span><br><span class="line"><span class="comment">在jQuery中把事件封装成了一个函数,事件名去掉on</span></span><br><span class="line"><span class="comment">鼠标点击事件就是: click(function() &#123; ... &#125;)</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line">$div.click(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="comment">/* </span></span><br><span class="line"><span class="comment">	在jQuery中把,获取样式和设置样式封装成了一个函数</span></span><br><span class="line"><span class="comment">	获取样式: jQuery对象.css(&#x27;属性名&#x27;)</span></span><br><span class="line"><span class="comment">	设置延时: jQuery对象.css(&#x27;属性名&#x27;,&#x27;属性值&#x27;)</span></span><br><span class="line"><span class="comment">	*/</span></span><br><span class="line">    $(<span class="built_in">this</span>).css(<span class="string">&#x27;background-color&#x27;</span>,<span class="string">&#x27;red&#x27;</span>);</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>

<img src= "https://cdn.jsdelivr.net/gh/Cai2w/cdn/img/loading.gif" data-lazy-src="D:\blog\source\_posts\04-jQuery.assets\image-20211110164810407.png" alt="image-20211110164810407" style="zoom:33%;" />    



<blockquote>
<p>代码实例: <strong>标签选择器</strong></p>
</blockquote>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">style</span> <span class="attr">type</span>=<span class="string">&quot;text/css&quot;</span>&gt;</span><span class="css"></span></span><br><span class="line"><span class="css">    <span class="selector-tag">div</span> &#123;</span></span><br><span class="line"><span class="css">        <span class="attribute">width</span>: <span class="number">200px</span>;</span></span><br><span class="line"><span class="css">        <span class="attribute">height</span>: <span class="number">200px</span>;</span></span><br><span class="line"><span class="css">        <span class="attribute">background-color</span>: <span class="number">#0000FF</span>;</span></span><br><span class="line"><span class="css">        <span class="attribute">border</span>: solid black;</span></span><br><span class="line"><span class="css">    &#125;</span></span><br><span class="line"><span class="css"></span><span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>结构</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">button</span> <span class="attr">id</span>=<span class="string">&quot;btn&quot;</span>&gt;</span>我是btn按钮<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span>&gt;</span>我是第1个div<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span>&gt;</span>我是第2个div<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span>&gt;</span>我是第3个div<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>脚本</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">type</span>=<span class="string">&quot;text/javascript&quot;</span>&gt;</span><span class="javascript"></span></span><br><span class="line"><span class="javascript">    <span class="comment">// 获取id为btn的按钮</span></span></span><br><span class="line"><span class="javascript">    <span class="keyword">var</span> $btn = $(<span class="string">&#x27;#btn&#x27;</span>);</span></span><br><span class="line"><span class="javascript">    <span class="comment">// 为按钮添加点击事件</span></span></span><br><span class="line"><span class="javascript">    $btn.click(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">        <span class="comment">// 获取所有的div改样式</span></span></span><br><span class="line"><span class="javascript">        $(<span class="string">&#x27;div&#x27;</span>).css(<span class="string">&#x27;background-color&#x27;</span>,<span class="string">&#x27;red&#x27;</span>);</span></span><br><span class="line"><span class="javascript">    &#125;);</span></span><br><span class="line"><span class="javascript"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p><img src= "https://cdn.jsdelivr.net/gh/Cai2w/cdn/img/loading.gif" data-lazy-src="D:\blog\source\_posts\04-jQuery.assets\image-20211110165646642.png" alt="image-20211110165646642" style="zoom:50%;" />        点击按钮后    <img src= "https://cdn.jsdelivr.net/gh/Cai2w/cdn/img/loading.gif" data-lazy-src="D:\blog\source\_posts\04-jQuery.assets\image-20211110165706662.png" alt="image-20211110165706662" style="zoom:50%;" /></p>
<blockquote>
<p>代码实例 : <strong>类选择器</strong></p>
</blockquote>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">button</span> <span class="attr">id</span>=<span class="string">&quot;btn2&quot;</span>&gt;</span>我是btn2按钮<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;mini&quot;</span>&gt;</span>我是class为mini的div<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;mini&quot;</span>&gt;</span>我是class为mini的div<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;mini&quot;</span>&gt;</span>我是class为mini的div<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>样式</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">style</span> <span class="attr">type</span>=<span class="string">&quot;text/css&quot;</span>&gt;</span><span class="css"></span></span><br><span class="line"><span class="css">    <span class="selector-class">.mini</span> &#123;</span></span><br><span class="line"><span class="css">        <span class="attribute">display</span>: inline-block;</span></span><br><span class="line"><span class="css">        <span class="attribute">width</span>: <span class="number">200px</span>;</span></span><br><span class="line"><span class="css">        <span class="attribute">height</span>: <span class="number">100px</span>;</span></span><br><span class="line"><span class="css">        <span class="attribute">background-color</span>: <span class="number">#0000FF</span>;</span></span><br><span class="line"><span class="css">        <span class="attribute">border</span>: solid black;</span></span><br><span class="line"><span class="css">    &#125;</span></span><br><span class="line"><span class="css"></span><span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>脚本</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">$(<span class="string">&#x27;#btn2&#x27;</span>).click(<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    $(<span class="string">&#x27;.mini&#x27;</span>).css(<span class="string">&#x27;background-color&#x27;</span>,<span class="string">&#x27;red&#x27;</span>);</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>

<p>点击前<img src= "https://cdn.jsdelivr.net/gh/Cai2w/cdn/img/loading.gif" data-lazy-src="D:\blog\source\_posts\04-jQuery.assets\image-20211110170255419.png" alt="image-20211110170255419" style="zoom:50%;" /></p>
<p>点击后<img src= "https://cdn.jsdelivr.net/gh/Cai2w/cdn/img/loading.gif" data-lazy-src="D:\blog\source\_posts\04-jQuery.assets\image-20211110170311854.png" alt="image-20211110170311854" style="zoom: 50%;" /></p>
<h2 id="2-2-层级选择器"><a href="#2-2-层级选择器" class="headerlink" title="2,2 层级选择器"></a>2,2 层级选择器</h2><ul>
<li><strong>语法 :</strong><ul>
<li>**$(‘A B’) : **获取A元素中的所有的B元素 , 后代选择器(包含子孙)</li>
<li>**$(‘A &gt; B’) : **获取A元素后的所有==子元素==B , 父子关系</li>
<li>**$(‘A + B’) : **获得的是A元素同级的下一个B元素 , 兄弟关系</li>
<li>**$(‘A ~ B’) : **获取A元素后面所有的同级的B元素 , 兄弟关系</li>
<li>**$(‘A’).siblings(‘B’) : **获取与A元素同级的所有的B元素</li>
</ul>
</li>
</ul>
<blockquote>
<p>代码实例</p>
</blockquote>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">div</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">button</span> <span class="attr">id</span>=<span class="string">&quot;b1&quot;</span>&gt;</span>改变father中所有的son<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">button</span> <span class="attr">id</span>=<span class="string">&quot;b2&quot;</span>&gt;</span>改变father中子元素son<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">button</span> <span class="attr">id</span>=<span class="string">&quot;b3&quot;</span>&gt;</span>改变father的下一个uncle<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">button</span> <span class="attr">id</span>=<span class="string">&quot;b4&quot;</span>&gt;</span>改变father后所有的uncle<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">button</span> <span class="attr">id</span>=<span class="string">&quot;b5&quot;</span>&gt;</span>改变father同级所有的uncle<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;uncle&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;father&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;son&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;bor&quot;</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;son&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;uncle&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;uncle&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>样式</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">style</span> <span class="attr">type</span>=<span class="string">&quot;text/css&quot;</span>&gt;</span><span class="css"></span></span><br><span class="line"><span class="css">    <span class="selector-class">.father</span> &#123;</span></span><br><span class="line"><span class="css">        <span class="attribute">float</span>: left;</span></span><br><span class="line"><span class="css">        <span class="attribute">width</span>: <span class="number">500px</span>;</span></span><br><span class="line"><span class="css">        <span class="attribute">height</span>: <span class="number">500px</span>;</span></span><br><span class="line"><span class="css">        <span class="attribute">border</span>: solid;</span></span><br><span class="line"><span class="css">        <span class="attribute">background-color</span>: green;</span></span><br><span class="line"><span class="css">    &#125;</span></span><br><span class="line"><span class="css">    <span class="selector-class">.bro</span> &#123;</span></span><br><span class="line"><span class="css">        <span class="attribute">float</span>: left;</span></span><br><span class="line"><span class="css">        <span class="attribute">width</span>: <span class="number">150px</span>;</span></span><br><span class="line"><span class="css">        <span class="attribute">height</span>: <span class="number">150px</span>;</span></span><br><span class="line"><span class="css">        <span class="attribute">background-color</span>: yellow;</span></span><br><span class="line"><span class="css">    &#125;</span></span><br><span class="line"><span class="css">    <span class="selector-class">.son</span> &#123;</span></span><br><span class="line"><span class="css">        <span class="attribute">width</span>: <span class="number">100px</span>;</span></span><br><span class="line"><span class="css">        <span class="attribute">height</span>: <span class="number">100px</span>;</span></span><br><span class="line"><span class="css">        <span class="attribute">background-color</span>: blue;</span></span><br><span class="line"><span class="css">        <span class="attribute">margin-top</span>: <span class="number">10px</span>;</span></span><br><span class="line"><span class="css">    &#125;</span></span><br><span class="line"><span class="css">    <span class="selector-class">.uncle</span> &#123;</span></span><br><span class="line"><span class="css">        <span class="attribute">float</span>: left;</span></span><br><span class="line"><span class="css">        <span class="attribute">width</span>: <span class="number">300px</span>;</span></span><br><span class="line"><span class="css">        <span class="attribute">height</span>: <span class="number">300px</span>;</span></span><br><span class="line"><span class="css">        <span class="attribute">margin</span>: <span class="number">20px</span>;</span></span><br><span class="line"><span class="css">        <span class="attribute">background-color</span>: orange;</span></span><br><span class="line"><span class="css">    &#125;</span></span><br><span class="line"><span class="css"></span><span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>脚本</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">type</span>=<span class="string">&quot;text/javascript&quot;</span>&gt;</span><span class="javascript"></span></span><br><span class="line"><span class="javascript">    <span class="comment">// 使用JQuery的页面加载事件</span></span></span><br><span class="line"><span class="javascript">    $(<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span></span><br><span class="line"><span class="javascript">        <span class="comment">// 改变father中所有的son</span></span></span><br><span class="line"><span class="javascript">        $(<span class="string">&#x27;#b1&#x27;</span>).click(<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span></span><br><span class="line"><span class="javascript">            $(<span class="string">&#x27;.father .son&#x27;</span>).css(<span class="string">&#x27;background-color&#x27;</span>,<span class="string">&#x27;red&#x27;</span>);</span></span><br><span class="line"><span class="javascript">        &#125;);</span></span><br><span class="line"><span class="javascript">        <span class="comment">// 改变father中子元素son</span></span></span><br><span class="line"><span class="javascript">        $(<span class="string">&#x27;#b2&#x27;</span>).click(<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span></span><br><span class="line"><span class="javascript">            $(<span class="string">&#x27;.father &gt; .son&#x27;</span>).css(<span class="string">&#x27;background-color&#x27;</span>,<span class="string">&#x27;red&#x27;</span>);</span></span><br><span class="line"><span class="javascript">        &#125;);</span></span><br><span class="line"><span class="javascript">        <span class="comment">// 改变father的下一个uncle</span></span></span><br><span class="line"><span class="javascript">        $(<span class="string">&#x27;#b3&#x27;</span>).click(<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span></span><br><span class="line"><span class="javascript">            $(<span class="string">&#x27;.father + .uncle&#x27;</span>).css(<span class="string">&#x27;background-color&#x27;</span>,<span class="string">&#x27;red&#x27;</span>);</span></span><br><span class="line"><span class="javascript">        &#125;);</span></span><br><span class="line"><span class="javascript">        <span class="comment">// 改变father后所有的uncle</span></span></span><br><span class="line"><span class="javascript">        $(<span class="string">&#x27;#b4&#x27;</span>).click(<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span></span><br><span class="line"><span class="javascript">            $(<span class="string">&#x27;.father ~ .uncle&#x27;</span>).css(<span class="string">&#x27;background-color&#x27;</span>,<span class="string">&#x27;red&#x27;</span>);</span></span><br><span class="line"><span class="javascript">        &#125;);</span></span><br><span class="line"><span class="javascript">        <span class="comment">// 改变father同级所有的uncle</span></span></span><br><span class="line"><span class="javascript">        $(<span class="string">&#x27;#b5&#x27;</span>).click(<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span></span><br><span class="line"><span class="javascript">            $(<span class="string">&#x27;.father&#x27;</span>).siblings(<span class="string">&#x27;.uncle&#x27;</span>).css(<span class="string">&#x27;background-color&#x27;</span>,<span class="string">&#x27;red&#x27;</span>);</span></span><br><span class="line"><span class="javascript">        &#125;);</span></span><br><span class="line"><span class="javascript">    &#125;);</span></span><br><span class="line"><span class="javascript"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>





<h2 id="2-3-属性选择器"><a href="#2-3-属性选择器" class="headerlink" title="2,3 属性选择器"></a>2,3 属性选择器</h2><ul>
<li><strong>语法 :</strong><ul>
<li>**$(‘A[属性名]’) : **选中所有拥有对应属性的 A 元素</li>
<li>**$(‘A[属性名=值]’) : **选中所有拥有对应属性和对应属性值的 A 元素</li>
<li><strong>$(‘A[属性名=值]…[属性名=值]’) :</strong> 复合属性选择器 , 多个属性同时选中</li>
<li>**$(‘A[属性名!=值]’) : **获取属性名不等于值得A元素</li>
<li>**$(‘A[属性名^=值]’) : **获得属性名以值开头的A元素</li>
<li>**$(‘A[属性名$=值]’) : **获得属性名以值结尾的A元素</li>
<li>**$(‘A[属性名*=值]’) : **获得属性名含有值的A元素</li>
</ul>
</li>
</ul>
<blockquote>
<p>代码实例</p>
</blockquote>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">div</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">button</span> <span class="attr">id</span>=<span class="string">&quot;b1&quot;</span>&gt;</span>改变所有拥有name属性文本框中内容<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">button</span> <span class="attr">id</span>=<span class="string">&quot;b2&quot;</span>&gt;</span>改变所有name属性值为pwd中的内容<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">button</span> <span class="attr">id</span>=<span class="string">&quot;b3&quot;</span>&gt;</span>改变所有type属性值为text,name属性值为msg的内容<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">button</span> <span class="attr">id</span>=<span class="string">&quot;b4&quot;</span>&gt;</span>改变所有不是文本框中的元素的内容<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">button</span> <span class="attr">id</span>=<span class="string">&quot;b5&quot;</span>&gt;</span>改变name属性值以text开头的元素的内容<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">button</span> <span class="attr">id</span>=<span class="string">&quot;b6&quot;</span>&gt;</span>改变name属性值以data结尾的元素的内容<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">button</span> <span class="attr">id</span>=<span class="string">&quot;b7&quot;</span>&gt;</span>改变class属性值包含in的元素内容<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">h4</span>&gt;</span>type=text,name是text开头<span class="tag">&lt;/<span class="name">h4</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">&quot;text&quot;</span> <span class="attr">name</span>=<span class="string">&quot;text1&quot;</span> /&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">&quot;text&quot;</span> <span class="attr">name</span>=<span class="string">&quot;text2&quot;</span> /&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">&quot;text&quot;</span> <span class="attr">name</span>=<span class="string">&quot;text3&quot;</span> /&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">h4</span>&gt;</span>type不是text,name是以data结尾<span class="tag">&lt;/<span class="name">h4</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">&quot;password&quot;</span> <span class="attr">name</span>=<span class="string">&quot;info-data&quot;</span>/&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">&quot;password&quot;</span> <span class="attr">name</span>=<span class="string">&quot;msg-data&quot;</span>/&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">h4</span>&gt;</span>type=text&amp;name=msg<span class="tag">&lt;/<span class="name">h4</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">&quot;text&quot;</span> <span class="attr">name</span>=<span class="string">&quot;msg&quot;</span> /&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">&quot;text&quot;</span> <span class="attr">name</span>=<span class="string">&quot;msg&quot;</span> /&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">h4</span>&gt;</span>name=pwd<span class="tag">&lt;/<span class="name">h4</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">&quot;text&quot;</span> <span class="attr">name</span>=<span class="string">&quot;pwd&quot;</span> /&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">&quot;text&quot;</span> <span class="attr">name</span>=<span class="string">&quot;pwd&quot;</span> /&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">h4</span>&gt;</span>没有name属性,class包含in<span class="tag">&lt;/<span class="name">h4</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">&quot;text&quot;</span> <span class="attr">class</span>=<span class="string">&quot;in-line&quot;</span>/&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">&quot;text&quot;</span> <span class="attr">class</span>=<span class="string">&quot;content-in&quot;</span>/&gt;</span></span><br></pre></td></tr></table></figure>

<p>脚本</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">type</span>=<span class="string">&quot;text/javascript&quot;</span>&gt;</span><span class="javascript"></span></span><br><span class="line"><span class="javascript">    <span class="comment">// jQuery的页面加载事件</span></span></span><br><span class="line"><span class="javascript">    $(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">        <span class="comment">//改变所有拥有name属性文本框中内容</span></span></span><br><span class="line"><span class="javascript">        $(<span class="string">&#x27;#b1&#x27;</span>).click(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">            $(<span class="string">&#x27;input[name]&#x27;</span>).val(<span class="string">&#x27;通过属性选择器添加的内容&#x27;</span>);</span></span><br><span class="line"><span class="javascript">        &#125;);</span></span><br><span class="line"><span class="javascript">        <span class="comment">//改变所有name属性值为pwd中的内容</span></span></span><br><span class="line"><span class="javascript">        $(<span class="string">&#x27;#b2&#x27;</span>).click(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">            $(<span class="string">&#x27;input[name=pwd]&#x27;</span>).val(<span class="string">&#x27;通过属性选择器添加的内容&#x27;</span>);</span></span><br><span class="line"><span class="javascript">        &#125;);</span></span><br><span class="line"><span class="javascript">        <span class="comment">//改变所有type属性值为text,name属性值为msg的内容</span></span></span><br><span class="line"><span class="javascript">        $(<span class="string">&#x27;#b3&#x27;</span>).click(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">            $(<span class="string">&#x27;input[type=text][name=msg]&#x27;</span>).val(<span class="string">&#x27;通过属性选择器添加的内容&#x27;</span>);</span></span><br><span class="line"><span class="javascript">        &#125;);</span></span><br><span class="line"><span class="javascript">        <span class="comment">//改变所有不是文本框中的元素的内容</span></span></span><br><span class="line"><span class="javascript">        $(<span class="string">&#x27;#b4&#x27;</span>).click(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">            $(<span class="string">&#x27;input[type!=text]&#x27;</span>).val(<span class="string">&#x27;通过属性选择器添加的内容&#x27;</span>);</span></span><br><span class="line"><span class="javascript">        &#125;);</span></span><br><span class="line"><span class="javascript">        <span class="comment">//改变name属性值以text开头的元素的内容</span></span></span><br><span class="line"><span class="javascript">        $(<span class="string">&#x27;#b5&#x27;</span>).click(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">            $(<span class="string">&#x27;input[name^=text]&#x27;</span>).val(<span class="string">&#x27;通过属性选择器添加的内容&#x27;</span>);</span></span><br><span class="line"><span class="javascript">        &#125;);</span></span><br><span class="line"><span class="javascript">        <span class="comment">//改变name属性值以data结尾的元素的内容</span></span></span><br><span class="line"><span class="javascript">        $(<span class="string">&#x27;#b6&#x27;</span>).click(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">            $(<span class="string">&#x27;input[name$=data]&#x27;</span>).val(<span class="string">&#x27;通过属性选择器添加的内容&#x27;</span>);</span></span><br><span class="line"><span class="javascript">        &#125;);</span></span><br><span class="line"><span class="javascript">        <span class="comment">//改变class属性值包含in的元素内容</span></span></span><br><span class="line"><span class="javascript">        $(<span class="string">&#x27;#b7&#x27;</span>).click(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">            $(<span class="string">&#x27;input[class*=in]&#x27;</span>).val(<span class="string">&#x27;通过属性选择器添加的内容&#x27;</span>);</span></span><br><span class="line"><span class="javascript">        &#125;);</span></span><br><span class="line"><span class="javascript">    &#125;)</span></span><br><span class="line"><span class="javascript"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>



<h2 id="2-4-基本过滤选择器"><a href="#2-4-基本过滤选择器" class="headerlink" title="2,4 基本过滤选择器"></a>2,4 基本过滤选择器</h2><ul>
<li><strong>语法</strong><ul>
<li>**:first  **获取选中元素中的第一个</li>
<li>**:last  **获取选中元素中的最后一个</li>
<li>**:not(selector)  **选中不包含指定内容的元素</li>
<li>**:even  **偶数 , 从0开始计算</li>
<li>**:odd  ** 奇数 , 从0开始算</li>
<li>**:eq(index)  **选中指定索引的元素</li>
<li>**:gt(index)   **选中大于指定索引的元素</li>
<li>**:lt(index)  **选中小于指定索引的元素</li>
<li>**:header  **获取所有的标题 h1 ~ h6</li>
</ul>
</li>
</ul>
<blockquote>
<p>代码实例</p>
</blockquote>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">button</span> <span class="attr">id</span>=<span class="string">&quot;b1&quot;</span>&gt;</span>选中第一个li标签,改变背景颜色<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">button</span> <span class="attr">id</span>=<span class="string">&quot;b2&quot;</span>&gt;</span>选中最后一个li标签,改变背景颜色<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">button</span> <span class="attr">id</span>=<span class="string">&quot;b3&quot;</span>&gt;</span>选中除类选择.item之外的元素,改变背景颜色<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">button</span> <span class="attr">id</span>=<span class="string">&quot;b4&quot;</span>&gt;</span>选中所有的偶数下标位置的li元素,改变背景颜色<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">button</span> <span class="attr">id</span>=<span class="string">&quot;b5&quot;</span>&gt;</span>选中所有的奇数下标位置的li元素,改变背景颜色<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">button</span> <span class="attr">id</span>=<span class="string">&quot;b6&quot;</span>&gt;</span>选中所有的下标位置为3的li元素,改变背景颜色<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">button</span> <span class="attr">id</span>=<span class="string">&quot;b7&quot;</span>&gt;</span>选中所有的下标位置大于3的li元素,改变背景颜色<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">button</span> <span class="attr">id</span>=<span class="string">&quot;b8&quot;</span>&gt;</span>选中所有的下标位置小于3的li元素,改变背景颜色<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">button</span> <span class="attr">id</span>=<span class="string">&quot;b9&quot;</span>&gt;</span>选中所有的标题元素,改变背景颜色<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">ul</span> <span class="attr">class</span>=<span class="string">&quot;list&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span>&gt;</span>我是第1个li标签<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span>&gt;</span>我是第2个li标签<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span>&gt;</span>我是第3个li标签<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span> <span class="attr">class</span>=<span class="string">&quot;item&quot;</span>&gt;</span>我是第4个li标签<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span> <span class="attr">class</span>=<span class="string">&quot;item&quot;</span>&gt;</span>我是第5个li标签<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span> <span class="attr">class</span>=<span class="string">&quot;item&quot;</span>&gt;</span>我是第6个li标签<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">ul</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">h1</span>&gt;</span>我是1级标题<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">h2</span>&gt;</span>我是2级标题<span class="tag">&lt;/<span class="name">h2</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">h3</span>&gt;</span>我是3级标题<span class="tag">&lt;/<span class="name">h3</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">h4</span>&gt;</span>我是4级标题<span class="tag">&lt;/<span class="name">h4</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">h5</span>&gt;</span>我是5级标题<span class="tag">&lt;/<span class="name">h5</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">h6</span>&gt;</span>我是6级标题<span class="tag">&lt;/<span class="name">h6</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>脚本</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">type</span>=<span class="string">&quot;text/javascript&quot;</span>&gt;</span><span class="javascript"></span></span><br><span class="line"><span class="javascript">    $(<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span></span><br><span class="line"><span class="javascript">        <span class="comment">// 选中第一个li标签,改变背景颜色</span></span></span><br><span class="line"><span class="javascript">        $(<span class="string">&#x27;#b1&#x27;</span>).click(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">            $(<span class="string">&#x27;li:first&#x27;</span>).css(<span class="string">&#x27;background-color&#x27;</span>,<span class="string">&#x27;skyblue&#x27;</span>);</span></span><br><span class="line"><span class="javascript">        &#125;);</span></span><br><span class="line"><span class="javascript">        <span class="comment">// 选中最后一个li标签,改变背景颜色</span></span></span><br><span class="line"><span class="javascript">        $(<span class="string">&#x27;#b2&#x27;</span>).click(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">            $(<span class="string">&#x27;li:last&#x27;</span>).css(<span class="string">&#x27;background-color&#x27;</span>,<span class="string">&#x27;skyblue&#x27;</span>);</span></span><br><span class="line"><span class="javascript">        &#125;);</span></span><br><span class="line"><span class="javascript">        <span class="comment">// 选中除类选择.item之外的li元素,改变背景颜色</span></span></span><br><span class="line"><span class="javascript">        $(<span class="string">&#x27;#b3&#x27;</span>).click(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">            $(<span class="string">&#x27;li:not(.item)&#x27;</span>).css(<span class="string">&#x27;background-color&#x27;</span>,<span class="string">&#x27;skyblue&#x27;</span>);</span></span><br><span class="line"><span class="javascript">        &#125;);</span></span><br><span class="line"><span class="javascript">        <span class="comment">// 选中所有的偶数下标位置的li元素,改变背景颜色</span></span></span><br><span class="line"><span class="javascript">        $(<span class="string">&#x27;#b4&#x27;</span>).click(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">            $(<span class="string">&#x27;li:even&#x27;</span>).css(<span class="string">&#x27;background-color&#x27;</span>,<span class="string">&#x27;skyblue&#x27;</span>);</span></span><br><span class="line"><span class="javascript">        &#125;);</span></span><br><span class="line"><span class="javascript">        <span class="comment">// 选中所有的奇数下标位置的li元素,改变背景颜色</span></span></span><br><span class="line"><span class="javascript">        $(<span class="string">&#x27;#b5&#x27;</span>).click(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">            $(<span class="string">&#x27;li:odd&#x27;</span>).css(<span class="string">&#x27;background-color&#x27;</span>,<span class="string">&#x27;pink&#x27;</span>);</span></span><br><span class="line"><span class="javascript">        &#125;);</span></span><br><span class="line"><span class="javascript">        <span class="comment">// 选中所有的下标位置为3的li元素,改变背景颜色</span></span></span><br><span class="line"><span class="javascript">        $(<span class="string">&#x27;#b6&#x27;</span>).click(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">            $(<span class="string">&#x27;li:eq(3)&#x27;</span>).css(<span class="string">&#x27;background-color&#x27;</span>,<span class="string">&#x27;pink&#x27;</span>);</span></span><br><span class="line"><span class="javascript">        &#125;);</span></span><br><span class="line"><span class="javascript">        <span class="comment">// 选中所有的下标位置大于3的li元素,改变背景颜色</span></span></span><br><span class="line"><span class="javascript">        $(<span class="string">&#x27;#b7&#x27;</span>).click(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">            $(<span class="string">&#x27;li:gt(3)&#x27;</span>).css(<span class="string">&#x27;background-color&#x27;</span>,<span class="string">&#x27;green&#x27;</span>);</span></span><br><span class="line"><span class="javascript">        &#125;);</span></span><br><span class="line"><span class="javascript">        <span class="comment">// 选中所有的下标位置小于3的li元素,改变背景颜色</span></span></span><br><span class="line"><span class="javascript">        $(<span class="string">&#x27;#b8&#x27;</span>).click(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">            $(<span class="string">&#x27;li:lt(3)&#x27;</span>).css(<span class="string">&#x27;background-color&#x27;</span>,<span class="string">&#x27;yellow&#x27;</span>);</span></span><br><span class="line"><span class="javascript">        &#125;);</span></span><br><span class="line"><span class="javascript">        <span class="comment">// 选中所有的标题元素,改变背景颜色</span></span></span><br><span class="line"><span class="javascript">        $(<span class="string">&#x27;#b9&#x27;</span>).click(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">            $(<span class="string">&#x27;:header&#x27;</span>).css(<span class="string">&#x27;background-color&#x27;</span>,<span class="string">&#x27;blue&#x27;</span>);</span></span><br><span class="line"><span class="javascript">        &#125;);</span></span><br><span class="line"><span class="javascript">    &#125;);</span></span><br><span class="line"><span class="javascript"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>



<h2 id="2-5-表单属性选择器"><a href="#2-5-表单属性选择器" class="headerlink" title="2,5 表单属性选择器"></a>2,5 表单属性选择器</h2><ul>
<li><strong>语法</strong><ul>
<li>**:enabled  **获取可用的元素</li>
<li>**:disabled  **获取不可用的元素</li>
<li>**:checked  **获取单选按钮 / 多选按钮 被选中的元素</li>
<li>**:selected  **获得下拉列表中的被选中的内容</li>
</ul>
</li>
</ul>
<blockquote>
<p>代码实例</p>
</blockquote>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">div</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">button</span> <span class="attr">id</span>=<span class="string">&quot;b1&quot;</span>&gt;</span>更改可用文本框中的value值<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">button</span> <span class="attr">id</span>=<span class="string">&quot;b2&quot;</span>&gt;</span>更改禁用文本框中的value值<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">button</span> <span class="attr">id</span>=<span class="string">&quot;b3&quot;</span>&gt;</span>获取选中的多选按钮的数量<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">button</span> <span class="attr">id</span>=<span class="string">&quot;b4&quot;</span>&gt;</span>获取选中下拉选框中的内容<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">&quot;text&quot;</span> /&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">&quot;text&quot;</span> <span class="attr">disabled</span>=<span class="string">&quot;disabled&quot;</span> /&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">br</span> /&gt;</span></span><br><span class="line">爱好:</span><br><span class="line"><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">&quot;checkbox&quot;</span> <span class="attr">name</span>=<span class="string">&quot;hob&quot;</span> <span class="attr">value</span>=<span class="string">&quot;lol&quot;</span> /&gt;</span>LOL</span><br><span class="line"><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">&quot;checkbox&quot;</span> <span class="attr">name</span>=<span class="string">&quot;hob&quot;</span> <span class="attr">value</span>=<span class="string">&quot;cs&quot;</span> /&gt;</span>CS</span><br><span class="line"><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">&quot;checkbox&quot;</span> <span class="attr">name</span>=<span class="string">&quot;hob&quot;</span> <span class="attr">value</span>=<span class="string">&quot;cod&quot;</span> /&gt;</span>COD</span><br><span class="line">籍贯:</span><br><span class="line"><span class="tag">&lt;<span class="name">select</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">option</span> <span class="attr">value</span> =<span class="string">&quot;LN&quot;</span>&gt;</span>辽宁<span class="tag">&lt;/<span class="name">option</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">option</span> <span class="attr">value</span> =<span class="string">&quot;HLJ&quot;</span>&gt;</span>黑龙江<span class="tag">&lt;/<span class="name">option</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">option</span> <span class="attr">value</span> =<span class="string">&quot;JL&quot;</span>&gt;</span>吉林<span class="tag">&lt;/<span class="name">option</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">option</span> <span class="attr">value</span> =<span class="string">&quot;NMG&quot;</span>&gt;</span>内蒙古<span class="tag">&lt;/<span class="name">option</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>脚本</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">type</span>=<span class="string">&quot;text/javascript&quot;</span>&gt;</span><span class="javascript"></span></span><br><span class="line"><span class="javascript">    $(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">        <span class="comment">// 更改文本框中的value值</span></span></span><br><span class="line"><span class="javascript">        $(<span class="string">&#x27;#b1&#x27;</span>).click(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">            <span class="comment">// $(&#x27;input[type=text]:enabled&#x27;).val(&#x27;哈哈哈哈哈&#x27;);</span></span></span><br><span class="line"><span class="javascript">            <span class="comment">// 通过jQuery获取input表单元素是可以简写的, : + type属性值</span></span></span><br><span class="line"><span class="javascript">            $(<span class="string">&#x27;:text:enabled&#x27;</span>).val(<span class="string">&#x27;哈哈哈哈哈&#x27;</span>);</span></span><br><span class="line"><span class="javascript">        &#125;);</span></span><br><span class="line"><span class="javascript">        <span class="comment">// 更改文本框中的value值</span></span></span><br><span class="line"><span class="javascript">        $(<span class="string">&#x27;#b2&#x27;</span>).click(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">            <span class="comment">// $(&#x27;input[type=text]:disabled&#x27;).val(&#x27;哈哈哈哈哈&#x27;);</span></span></span><br><span class="line"><span class="javascript">            $(<span class="string">&#x27;:text:disabled&#x27;</span>).val(<span class="string">&#x27;哈哈哈哈哈&#x27;</span>);</span></span><br><span class="line"><span class="javascript">        &#125;);</span></span><br><span class="line"><span class="javascript">        <span class="comment">// 获取选中的多选按钮的数量</span></span></span><br><span class="line"><span class="javascript">        $(<span class="string">&#x27;#b3&#x27;</span>).click(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">            <span class="keyword">var</span> $cks = $(<span class="string">&#x27;:checkbox:checked&#x27;</span>);</span></span><br><span class="line"><span class="javascript">            <span class="comment">// 打印获取的已选中的多选按钮数量</span></span></span><br><span class="line"><span class="javascript">            <span class="comment">// 通过 jQuery 对象的 length 属性获取</span></span></span><br><span class="line"><span class="javascript">            <span class="built_in">console</span>.log(<span class="string">&#x27;已选中 &#x27;</span> + $cks.length + <span class="string">&#x27; 项爱好&#x27;</span>);</span></span><br><span class="line"><span class="javascript">        &#125;);</span></span><br><span class="line"><span class="javascript">        <span class="comment">// 获取选中下拉选框中的内容</span></span></span><br><span class="line"><span class="javascript">        $(<span class="string">&#x27;#b4&#x27;</span>).click(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">            <span class="keyword">var</span> $op = $(<span class="string">&#x27;option:selected&#x27;</span>);</span></span><br><span class="line"><span class="javascript">            <span class="built_in">console</span>.log(<span class="string">&#x27;选中的籍贯:&#x27;</span> + $op.val());</span></span><br><span class="line"><span class="javascript">            <span class="built_in">console</span>.log(<span class="string">&#x27;选中的籍贯:&#x27;</span> + $op.text());</span></span><br><span class="line"><span class="javascript">        &#125;);</span></span><br><span class="line"><span class="javascript">    &#125;)</span></span><br><span class="line"><span class="javascript"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>





<h1 id="3-jQuery的DOM操作"><a href="#3-jQuery的DOM操作" class="headerlink" title="3, jQuery的DOM操作"></a>3, jQuery的DOM操作</h1><h2 id="3-1-jQuery操作元素内容"><a href="#3-1-jQuery操作元素内容" class="headerlink" title="3,1 jQuery操作元素内容"></a>3,1 jQuery操作元素内容</h2><ul>
<li>**text() : **获取或修改元素的文本内容 , 类似于 innerText</li>
<li>**html() : **获取或修改元素的标签体 , 类似于 innerHTML</li>
</ul>
<p><strong>注意 :</strong></p>
<ol>
<li>text() 获取和设置所有的</li>
<li>html() 获取第一个 , 设置所有的</li>
</ol>
<blockquote>
<p>代码实例</p>
</blockquote>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">span</span>&gt;</span>span1<span class="tag">&lt;/<span class="name">span</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">span</span>&gt;</span>span2<span class="tag">&lt;/<span class="name">span</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">span</span>&gt;</span>span3<span class="tag">&lt;/<span class="name">span</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span>&gt;</span>div1<span class="tag">&lt;<span class="name">br</span> /&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span>&gt;</span>div2<span class="tag">&lt;<span class="name">br</span> /&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span>&gt;</span>div3<span class="tag">&lt;<span class="name">br</span> /&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">type</span>=<span class="string">&quot;text/javascript&quot;</span>&gt;</span><span class="javascript"></span></span><br><span class="line"><span class="javascript">    <span class="comment">// 获取所有的span和div</span></span></span><br><span class="line"><span class="javascript">    <span class="keyword">var</span> $spa = $(<span class="string">&#x27;span&#x27;</span>);</span></span><br><span class="line"><span class="javascript">    <span class="keyword">var</span> $div = $(<span class="string">&#x27;div&#x27;</span>);</span></span><br><span class="line"><span class="javascript"></span></span><br><span class="line"><span class="javascript">    <span class="comment">// 1,通过 text() 获取文本内容</span></span></span><br><span class="line"><span class="javascript">    <span class="built_in">console</span>.log($spa.text());</span></span><br><span class="line"><span class="javascript">    <span class="built_in">console</span>.log($div.text());</span></span><br><span class="line"><span class="javascript"></span></span><br><span class="line"><span class="javascript">    <span class="comment">// 2,通过html()获取标签体</span></span></span><br><span class="line"><span class="javascript">    <span class="comment">// 通过html()获取的内容是第一个元素的内容</span></span></span><br><span class="line"><span class="javascript">    <span class="built_in">console</span>.log($spa.html());</span></span><br><span class="line"><span class="javascript">    <span class="built_in">console</span>.log($div.html());</span></span><br><span class="line"><span class="javascript"></span></span><br><span class="line"><span class="javascript">    <span class="comment">// 3,设置纯文本内容</span></span></span><br><span class="line"><span class="javascript">    $spa.text(<span class="string">&#x27;&lt;h1&gt;h1标题标签&lt;/h1&gt;&#x27;</span>);</span></span><br><span class="line"><span class="javascript"></span></span><br><span class="line"><span class="javascript">    <span class="comment">// 4,通过html() 设置div的标签体</span></span></span><br><span class="line"><span class="javascript">    $div.html(<span class="string">&#x27;&lt;h1&gt;h1标题标签&lt;/h1&gt;&#x27;</span>);</span></span><br><span class="line"><span class="javascript"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>



<h2 id="3-2-jQuery操作元素属性"><a href="#3-2-jQuery操作元素属性" class="headerlink" title="3,2 jQuery操作元素属性"></a>3,2 jQuery操作元素属性</h2><ul>
<li>**val() : **获取或者修改 value 属性值 , 等同于 ele.value</li>
<li>**prop() : **获取或者修改原生属性值</li>
<li>**attr() : **获取或者设置自定义属性 , 相当于 js 的 setAttribute(),getAttribute()</li>
<li>**removeAttr() : **通过属性名称删除属性值</li>
<li>**height() : **设置返回元素高度</li>
<li>**width() : **设置返回元素宽度</li>
</ul>
<blockquote>
<p>代码实例</p>
</blockquote>
<p>结构</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">&quot;text&quot;</span> <span class="attr">name</span>=<span class="string">&quot;username&quot;</span> <span class="attr">value</span>=<span class="string">&quot;张三&quot;</span>/&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">ul</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span> <span class="attr">id</span>=<span class="string">&quot;bj&quot;</span> <span class="attr">name</span>=<span class="string">&quot;beijing&quot;</span>&gt;</span>北京<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span> <span class="attr">id</span>=<span class="string">&quot;tj&quot;</span> <span class="attr">name</span>=<span class="string">&quot;tianjin&quot;</span>&gt;</span>天津<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">ul</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">&quot;checkbox&quot;</span> <span class="attr">name</span>=<span class="string">&quot;remember&quot;</span> <span class="attr">id</span>=<span class="string">&quot;rem&quot;</span> /&gt;</span>是否记住信息?</span><br></pre></td></tr></table></figure>

<p><strong>val()</strong></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 获取张三</span></span><br><span class="line"><span class="keyword">var</span> $in = $(<span class="string">&#x27;:text&#x27;</span>);</span><br><span class="line"><span class="comment">/* </span></span><br><span class="line"><span class="comment">jQuery将value属性封装成了一个函数</span></span><br><span class="line"><span class="comment">获取属性值: jQuery对象.val();</span></span><br><span class="line"><span class="comment">设置属性值: jQuery对象.val(值);</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="built_in">console</span>.log($in.val());</span><br><span class="line"></span><br><span class="line"><span class="comment">// 将张三改为李四</span></span><br><span class="line">$in.val(<span class="string">&#x27;李四&#x27;</span>);</span><br></pre></td></tr></table></figure>

<img src= "https://cdn.jsdelivr.net/gh/Cai2w/cdn/img/loading.gif" data-lazy-src="D:\blog\source\_posts\04-jQuery.assets\image-20211111155732362.png" alt="image-20211111155732362" style="zoom:50%;border: solid" />    

<p><strong>attr()</strong></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 获取 id 值 bj 的元素</span></span><br><span class="line"><span class="keyword">var</span> $bj = $(<span class="string">&#x27;#bj&#x27;</span>);</span><br><span class="line"><span class="comment">// 获取该对象的name属性值</span></span><br><span class="line"><span class="comment">/* </span></span><br><span class="line"><span class="comment">jQuery中将设置属性值和获取属性值封装成了函数</span></span><br><span class="line"><span class="comment">获取属性值: jQuery对象.attr(&#x27;属性名&#x27;);</span></span><br><span class="line"><span class="comment">设置属性值: jQuery对象.attr(&#x27;属性名&#x27;,&#x27;属性值&#x27;);</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="built_in">console</span>.log($bj.attr(<span class="string">&#x27;name&#x27;</span>));</span><br><span class="line"><span class="comment">// 设置北京的name属性值为addr-beijing</span></span><br><span class="line">$bj.attr(<span class="string">&#x27;name&#x27;</span>,<span class="string">&#x27;addr-beijing&#x27;</span>);</span><br><span class="line"><span class="comment">// 给北京元素新增一个disc属性,属性值为shoudu</span></span><br><span class="line">$bj.attr(<span class="string">&#x27;data-disc&#x27;</span>,<span class="string">&#x27;shoudu&#x27;</span>);</span><br></pre></td></tr></table></figure>

<p><img src= "https://cdn.jsdelivr.net/gh/Cai2w/cdn/img/loading.gif" data-lazy-src="D:\blog\source_posts\04-jQuery.assets\image-20211111160546361.png" alt="image-20211111160546361"></p>
<p><img src= "https://cdn.jsdelivr.net/gh/Cai2w/cdn/img/loading.gif" data-lazy-src="D:\blog\source_posts\04-jQuery.assets\image-20211111160555224.png" alt="image-20211111160555224">    </p>
<p><strong>removeAttr()</strong></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 删除北京的data-disc属性</span></span><br><span class="line">$bj.removeAttr(<span class="string">&#x27;data-disc&#x27;</span>);</span><br></pre></td></tr></table></figure>

<p><img src= "https://cdn.jsdelivr.net/gh/Cai2w/cdn/img/loading.gif" data-lazy-src="D:\blog\source_posts\04-jQuery.assets\image-20211111161011838.png" alt="image-20211111161011838">    </p>
<p><strong>prop()</strong></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 通过 attr 获取 checked 属性值</span></span><br><span class="line"><span class="built_in">console</span>.log($(<span class="string">&#x27;#rem&#x27;</span>).attr(<span class="string">&#x27;checked&#x27;</span>));<span class="comment">// 根据属性名称获取属性值</span></span><br><span class="line"><span class="comment">// 因为li元素没有name属性,所有获取不到</span></span><br><span class="line"><span class="built_in">console</span>.log($(<span class="string">&#x27;#tj&#x27;</span>).prop(<span class="string">&#x27;name&#x27;</span>));<span class="comment">// undefined</span></span><br><span class="line"><span class="comment">// 设置天津的class属性值为abc</span></span><br><span class="line">$(<span class="string">&#x27;#tj&#x27;</span>).prop(<span class="string">&#x27;class&#x27;</span>,<span class="string">&#x27;abc&#x27;</span>);</span><br><span class="line"><span class="comment">// 修改失败,因为获取不到name属性,因为name在次不是内置属性</span></span><br><span class="line">$(<span class="string">&#x27;#tj&#x27;</span>).prop(<span class="string">&#x27;name&#x27;</span>,<span class="string">&#x27;666&#x27;</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">/* </span></span><br><span class="line"><span class="comment">jQuery对象的.prop(&#x27;checked&#x27;);获取单选按钮或者多选按钮的选中状态: 选中:true反之false</span></span><br><span class="line"><span class="comment">jQuery对象的.prop(&#x27;selected&#x27;);获取下拉选框的选中状态 选中:true反之false</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="built_in">console</span>.log($(<span class="string">&#x27;#rem&#x27;</span>).prop(<span class="string">&#x27;checked&#x27;</span>));</span><br></pre></td></tr></table></figure>

<p><img src= "https://cdn.jsdelivr.net/gh/Cai2w/cdn/img/loading.gif" data-lazy-src="D:\blog\source_posts\04-jQuery.assets\image-20211111162150507.png" alt="image-20211111162150507">    </p>
<p><img src= "https://cdn.jsdelivr.net/gh/Cai2w/cdn/img/loading.gif" data-lazy-src="D:\blog\source_posts\04-jQuery.assets\image-20211111162127885.png" alt="image-20211111162127885">    </p>
<p><strong>width &amp; height</strong></p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;box&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">type</span>=<span class="string">&quot;text/javascript&quot;</span>&gt;</span><span class="javascript"></span></span><br><span class="line"><span class="javascript">    <span class="keyword">var</span> $box = $(<span class="string">&#x27;.box&#x27;</span>);</span></span><br><span class="line"><span class="javascript">    <span class="built_in">console</span>.log($box.height());</span></span><br><span class="line"><span class="javascript">    <span class="built_in">console</span>.log($box.width());</span></span><br><span class="line"><span class="javascript">    <span class="comment">// 设置元素的宽度和高度</span></span></span><br><span class="line"><span class="javascript">    $box.height(<span class="number">500</span>);</span></span><br><span class="line"><span class="javascript">    $box.width(<span class="number">500</span>);</span></span><br><span class="line"><span class="javascript">    $box.css(<span class="string">&#x27;background-color&#x27;</span>,<span class="string">&#x27;orange&#x27;</span>);</span></span><br><span class="line"><span class="javascript"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>

<img src= "https://cdn.jsdelivr.net/gh/Cai2w/cdn/img/loading.gif" data-lazy-src="D:\blog\source\_posts\04-jQuery.assets\image-20211115143346209.png" alt="image-20211115143346209" style="zoom:33%;border: solid" />    



<h2 id="3-3-jQuery操作样式"><a href="#3-3-jQuery操作样式" class="headerlink" title="3,3 jQuery操作样式"></a>3,3 jQuery操作样式</h2><ul>
<li><p><strong>css() :</strong> 获取或者设置 CSS 样式 </p>
<p>用法 :</p>
<ul>
<li>**css(样式名) : **获取一个样式 , 等于js的 ele.style.驼峰样式名</li>
<li>**css(样式名,样式值) : **设置一个样式 , 等于js 的 ele.style.驼峰样式名 = 样式值</li>
<li>**css({样式名:样式值,样式名:样式值}) : **批量设置多个样式</li>
</ul>
</li>
<li><p>**addClass() : **添加一个类样式</p>
</li>
<li><p>**removeClass() : **移除一个类样式</p>
</li>
<li><p>**toggleClass() : **切换一个样式</p>
</li>
</ul>
<blockquote>
<p>代码实例</p>
</blockquote>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">p</span> <span class="attr">id</span>=<span class="string">&quot;p1&quot;</span>&gt;</span>设置一个样式<span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">p</span> <span class="attr">id</span>=<span class="string">&quot;p2&quot;</span>&gt;</span>批量的设置样式<span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">type</span>=<span class="string">&quot;text/javascript&quot;</span>&gt;</span><span class="javascript"></span></span><br><span class="line"><span class="javascript">    <span class="comment">// 单独指定样式以及属性</span></span></span><br><span class="line"><span class="javascript">    <span class="keyword">var</span> $p1 = $(<span class="string">&#x27;#p1&#x27;</span>);</span></span><br><span class="line"><span class="javascript">    $p1.css(<span class="string">&#x27;color&#x27;</span>,<span class="string">&#x27;#FFFFFF&#x27;</span>);</span></span><br><span class="line"><span class="javascript">    $p1.css(<span class="string">&#x27;font-size&#x27;</span>,<span class="string">&#x27;30px&#x27;</span>);</span></span><br><span class="line"><span class="javascript">    $p1.css(<span class="string">&#x27;background-color&#x27;</span>,<span class="string">&#x27;blue&#x27;</span>);</span></span><br><span class="line"><span class="javascript"></span></span><br><span class="line"><span class="javascript">    <span class="keyword">var</span> $p2 = $(<span class="string">&#x27;#p2&#x27;</span>);</span></span><br><span class="line"><span class="javascript">    $p2.css(&#123;<span class="string">&#x27;background-color&#x27;</span>:<span class="string">&#x27;pink&#x27;</span>,<span class="string">&#x27;color&#x27;</span>:<span class="string">&#x27;green&#x27;</span>,<span class="string">&#x27;font-size&#x27;</span>:<span class="string">&#x27;35px&#x27;</span>&#125;);</span></span><br><span class="line"><span class="javascript"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>

<img src= "https://cdn.jsdelivr.net/gh/Cai2w/cdn/img/loading.gif" data-lazy-src="D:\blog\source\_posts\04-jQuery.assets\image-20211111165337761.png" alt="image-20211111165337761" style="zoom:50%;" />    



<p><strong>注意 : addClass 会将 class 进行叠加 , 不同 js 的直接覆盖 , 所以在某些情况下为了样式不冲突 , 需要通过removeClass删除</strong></p>
<p>结构</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">p</span> <span class="attr">id</span>=<span class="string">&quot;p1&quot;</span>&gt;</span>1,通过class属性设置样式<span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">p</span> <span class="attr">id</span>=<span class="string">&quot;p2&quot;</span>&gt;</span>2,通过按钮点击样式切换<span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">button</span> <span class="attr">id</span>=<span class="string">&quot;btn&quot;</span>&gt;</span>切换样式<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>样式</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">style</span> <span class="attr">type</span>=<span class="string">&quot;text/css&quot;</span>&gt;</span><span class="css"></span></span><br><span class="line"><span class="css">    <span class="selector-class">.font</span> &#123;</span></span><br><span class="line"><span class="css">        <span class="attribute">color</span>: aliceblue;</span></span><br><span class="line"><span class="css">        <span class="attribute">font</span>: <span class="number">700</span> <span class="number">40px</span> <span class="string">&#x27;楷体&#x27;</span>;</span></span><br><span class="line"><span class="css">        <span class="attribute">background-color</span>: blueviolet;</span></span><br><span class="line"><span class="css">    &#125;</span></span><br><span class="line"><span class="css">    <span class="selector-class">.ff</span> &#123;</span></span><br><span class="line"><span class="css">        <span class="attribute">color</span>: aqua;</span></span><br><span class="line"><span class="css">        <span class="attribute">font</span>: <span class="number">700</span> <span class="number">40px</span> <span class="string">&#x27;黑体&#x27;</span>;</span></span><br><span class="line"><span class="css">        <span class="attribute">background-color</span>: burlywood;</span></span><br><span class="line"><span class="css">    &#125;</span></span><br><span class="line"><span class="css"></span><span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>脚本</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">type</span>=<span class="string">&quot;text/javascript&quot;</span>&gt;</span><span class="javascript"></span></span><br><span class="line"><span class="javascript">    <span class="comment">// $(document).ready(f...)</span></span></span><br><span class="line"><span class="javascript">    <span class="comment">// $().ready(f...)</span></span></span><br><span class="line"><span class="javascript">    $(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">        <span class="comment">// 通过class属性改变元素的样式</span></span></span><br><span class="line"><span class="javascript">        <span class="keyword">var</span> $p1 = $(<span class="string">&#x27;#p1&#x27;</span>);</span></span><br><span class="line"><span class="javascript">        $p1.addClass(<span class="string">&#x27;font&#x27;</span>);<span class="comment">// 只写class选择器名,不写.</span></span></span><br><span class="line"><span class="javascript">        $p1.addClass(<span class="string">&#x27;ff&#x27;</span>);</span></span><br><span class="line"><span class="javascript">        <span class="comment">// 通过removeClass移除font</span></span></span><br><span class="line"><span class="javascript">        $p1.removeClass(<span class="string">&#x27;font&#x27;</span>);</span></span><br><span class="line"><span class="javascript"></span></span><br><span class="line"><span class="javascript">        <span class="comment">// 获取切换的按钮,绑定点击事件</span></span></span><br><span class="line"><span class="javascript">        $(<span class="string">&#x27;#btn&#x27;</span>).click(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">            <span class="comment">// 通过toggleClass,实现class属性值的切换</span></span></span><br><span class="line"><span class="javascript">            $(<span class="string">&#x27;#p2&#x27;</span>).toggleClass(<span class="string">&#x27;font&#x27;</span>);</span></span><br><span class="line"><span class="javascript">        &#125;);</span></span><br><span class="line"><span class="javascript">    &#125;);</span></span><br><span class="line"><span class="javascript"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>点击按钮前</p>
<p><img src= "https://cdn.jsdelivr.net/gh/Cai2w/cdn/img/loading.gif" data-lazy-src="D:\blog\source_posts\04-jQuery.assets\image-20211112095039378.png" alt="image-20211112095039378">    </p>
<p>点击按钮后</p>
<p><img src= "https://cdn.jsdelivr.net/gh/Cai2w/cdn/img/loading.gif" data-lazy-src="D:\blog\source_posts\04-jQuery.assets\image-20211112095056349.png" alt="image-20211112095056349">    </p>
<h2 id="3-4-jQuery创建和插入对象"><a href="#3-4-jQuery创建和插入对象" class="headerlink" title="3,4 jQuery创建和插入对象"></a>3,4 jQuery创建和插入对象</h2><ul>
<li><p><strong>语法 :</strong></p>
<ul>
<li><p>**append(ele) : **添加成最后一个子元素 , 两者之间为父子关系</p>
</li>
<li><p>**prepend(ele) : **添加成第一个子元素 , 两者为父子关系</p>
</li>
<li><p>jQuery 创建元素</p>
<p><strong>$(‘&lt;a href=”#”&gt;超链接&lt;/a&gt;’)</strong> 代表创建一个超链接元素 , 同时给对象天加标签体和属性</p>
</li>
<li><p>**before(ele) : **将元素添加至当前元素的前方 , 兄弟关系 </p>
</li>
<li><p>**after(ele) : **将元素添加至当前元素的后方 , 兄弟关系 </p>
</li>
<li><p>**insertBefore(ele) : **把当前元素添加到指定元素的前方 , 兄弟关系</p>
</li>
<li><p>**insertAfter(ele) : **把当前元素添加到指定元素的后方 , 兄弟关系</p>
</li>
</ul>
</li>
</ul>
<blockquote>
<p>代码实例</p>
</blockquote>
<p>结构</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">ul</span> <span class="attr">id</span>=<span class="string">&quot;city&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span> <span class="attr">id</span>=<span class="string">&quot;bj&quot;</span> <span class="attr">name</span>=<span class="string">&quot;beijing&quot;</span>&gt;</span>北京<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span> <span class="attr">id</span>=<span class="string">&quot;tj&quot;</span> <span class="attr">name</span>=<span class="string">&quot;tianjin&quot;</span>&gt;</span>天津<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span> <span class="attr">id</span>=<span class="string">&quot;cq&quot;</span> <span class="attr">name</span>=<span class="string">&quot;chongqing&quot;</span>&gt;</span>重庆<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">ul</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">ul</span> <span class="attr">id</span>=<span class="string">&quot;hob&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span> <span class="attr">id</span>=<span class="string">&quot;fk&quot;</span> <span class="attr">name</span>=<span class="string">&quot;fankong&quot;</span>&gt;</span>反恐<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span> <span class="attr">id</span>=<span class="string">&quot;xj&quot;</span> <span class="attr">name</span>=<span class="string">&quot;xingji&quot;</span>&gt;</span>星际<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span> <span class="attr">id</span>=<span class="string">&quot;lm&quot;</span> <span class="attr">name</span>=<span class="string">&quot;lianmeng&quot;</span>&gt;</span>联盟<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">ul</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>脚本</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">type</span>=<span class="string">&quot;text/javascript&quot;</span>&gt;</span><span class="javascript"></span></span><br><span class="line"><span class="javascript">    <span class="comment">// 将hob中的反恐添加city子元素后面</span></span></span><br><span class="line"><span class="javascript">    <span class="comment">// var $city = $(&#x27;#city&#x27;);</span></span></span><br><span class="line"><span class="javascript">    <span class="comment">// var $fk = $(&#x27;#fk&#x27;);</span></span></span><br><span class="line"><span class="javascript">    <span class="comment">// 使用父元素对象调用 append() 将其添加为子元素的最后一个</span></span></span><br><span class="line"><span class="javascript">    <span class="comment">// $city.append($fk);</span></span></span><br><span class="line"><span class="javascript">    $(<span class="string">&#x27;#city&#x27;</span>).append($(<span class="string">&#x27;#fk&#x27;</span>)); <span class="comment">// 链式编程</span></span></span><br><span class="line"><span class="javascript"></span></span><br><span class="line"><span class="javascript">    <span class="comment">// 将hob中的星际加到子元素的第一个</span></span></span><br><span class="line"><span class="javascript">    $(<span class="string">&#x27;#city&#x27;</span>).prepend($(<span class="string">&#x27;#xj&#x27;</span>));</span></span><br><span class="line"><span class="javascript"></span></span><br><span class="line"><span class="javascript">    <span class="comment">// 通过jQuery创建一个元素 li标签,内容魔兽,name=moshou,id=ms</span></span></span><br><span class="line"><span class="javascript">    <span class="keyword">var</span> $ms = $(<span class="string">&#x27;&lt;li id=&quot;mx&quot; name=&quot;moushou&quot;&gt;魔兽&lt;/li&gt;&#x27;</span>);</span></span><br><span class="line"><span class="javascript">    <span class="built_in">console</span>.log($ms);</span></span><br><span class="line"><span class="javascript">    <span class="comment">// 将魔兽添加到联盟的前面</span></span></span><br><span class="line"><span class="javascript">    <span class="comment">// $(&#x27;#lm&#x27;).before($ms);// 在联盟前面添加魔兽</span></span></span><br><span class="line"><span class="javascript">    $ms.insertBefore($(<span class="string">&#x27;#lm&#x27;</span>));<span class="comment">// 把魔兽添加到联盟的前面</span></span></span><br><span class="line"><span class="javascript"></span></span><br><span class="line"><span class="javascript">    <span class="comment">// 通过jQuery创建一个元素 li标签,内容荣耀,name=rongyao,id=ry</span></span></span><br><span class="line"><span class="javascript">    <span class="keyword">var</span> $ry = $(<span class="string">&#x27;&lt;li id=&quot;ry&quot; name=&quot;rongyao&quot;&gt;荣耀&lt;/li&gt;&#x27;</span>);</span></span><br><span class="line"><span class="javascript">    <span class="comment">// 将荣耀添加到联盟的后面</span></span></span><br><span class="line"><span class="javascript">    <span class="comment">// $(&#x27;#lm&#x27;).after($ry);// 在联盟的后面添加荣耀</span></span></span><br><span class="line"><span class="javascript">    $ry.insertAfter($(<span class="string">&#x27;#lm&#x27;</span>));<span class="comment">// 把荣耀添加到联盟的后面</span></span></span><br><span class="line"><span class="javascript"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>

<img src= "https://cdn.jsdelivr.net/gh/Cai2w/cdn/img/loading.gif" data-lazy-src="D:\blog\source\_posts\04-jQuery.assets\image-20211112101806858.png" alt="image-20211112101806858" style="zoom:50%;border: solid" />    



<p><strong>注意 : 区分 after 和 insertAfter</strong></p>
<ul>
<li>**A.after(B) : **在A的后面添加B</li>
<li>**A.insertAfter(B) : **把A添加到B后面</li>
</ul>
<ul>
<li>**appendTo(ele) : **将元素添加进入对应的元素中 , 为最后一个子元素</li>
<li>**prependTo(ele) : **将元素添加进入对应的元素中 , 为第一个子元素</li>
</ul>
<p>==注意 : 区分append 和 appendTo==</p>
<ul>
<li>**A.append (B) : **在A的中添加B,位置是最后一个子元素</li>
<li>**A.appendTo(B) : **把A添加到B中,位置是最后一个子元素</li>
</ul>
<blockquote>
<p>代码实例</p>
</blockquote>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">ul</span> <span class="attr">id</span>=<span class="string">&quot;list&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span> <span class="attr">id</span>=<span class="string">&quot;news&quot;</span>&gt;</span>新闻<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span> <span class="attr">id</span>=<span class="string">&quot;video&quot;</span>&gt;</span>视频<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span> <span class="attr">id</span>=<span class="string">&quot;vlog&quot;</span>&gt;</span>vlog<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span> <span class="attr">id</span>=<span class="string">&quot;yl&quot;</span>&gt;</span>娱乐<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span> <span class="attr">id</span>=<span class="string">&quot;js&quot;</span>&gt;</span>军事<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">ul</span>&gt;</span></span><br></pre></td></tr></table></figure>

<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">// 通过 appendTo 在军事的后面加入&quot;疫情&quot;</span><br><span class="line">// 解释: 将疫情的li元素,添加进入ul中,位置为最后一个子元素</span><br><span class="line">$(&#x27;<span class="tag">&lt;<span class="name">li</span> <span class="attr">id</span>=<span class="string">&quot;yq&quot;</span>&gt;</span>疫情<span class="tag">&lt;/<span class="name">li</span>&gt;</span>&#x27;).appendTo($(&#x27;#list&#x27;));</span><br><span class="line"></span><br><span class="line">// 通过 prependTo 在新闻前面加入&quot;体育&quot;</span><br><span class="line">$(&#x27;<span class="tag">&lt;<span class="name">li</span> <span class="attr">id</span>=<span class="string">&quot;ty&quot;</span>&gt;</span>体育<span class="tag">&lt;/<span class="name">li</span>&gt;</span>&#x27;).prependTo($(&#x27;#list&#x27;));</span><br></pre></td></tr></table></figure>

<img src= "https://cdn.jsdelivr.net/gh/Cai2w/cdn/img/loading.gif" data-lazy-src="D:\blog\source\_posts\04-jQuery.assets\image-20211112135301897.png" alt="image-20211112135301897" style="zoom:67%;border: solid" />    



<h2 id="3-5-jQuery删除对象"><a href="#3-5-jQuery删除对象" class="headerlink" title="3,5 jQuery删除对象"></a>3,5 jQuery删除对象</h2><ul>
<li><strong>语法</strong><ul>
<li>**remove() : **删除指定的元素</li>
<li>**empty() : **清空指定元素的所有子元素</li>
</ul>
</li>
</ul>
<blockquote>
<p>代码实例</p>
</blockquote>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">ul</span> <span class="attr">id</span>=<span class="string">&quot;list&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span> <span class="attr">id</span>=<span class="string">&quot;news&quot;</span>&gt;</span>新闻<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span> <span class="attr">id</span>=<span class="string">&quot;video&quot;</span>&gt;</span>视频<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span> <span class="attr">id</span>=<span class="string">&quot;vlog&quot;</span>&gt;</span>vlog<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span> <span class="attr">id</span>=<span class="string">&quot;yl&quot;</span>&gt;</span>娱乐<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span> <span class="attr">id</span>=<span class="string">&quot;js&quot;</span>&gt;</span>军事<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">ul</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">type</span>=<span class="string">&quot;text/javascript&quot;</span>&gt;</span><span class="javascript"></span></span><br><span class="line"><span class="javascript">    <span class="comment">// 删除ul中的vlog</span></span></span><br><span class="line"><span class="javascript">    <span class="comment">// 通过remove()删除自己</span></span></span><br><span class="line"><span class="javascript">    $(<span class="string">&#x27;#vlog&#x27;</span>).remove();</span></span><br><span class="line"><span class="javascript"></span></span><br><span class="line"><span class="javascript">    <span class="comment">// 清空所有的子元素节点</span></span></span><br><span class="line"><span class="javascript">    $(<span class="string">&#x27;#list&#x27;</span>).empty();</span></span><br><span class="line"><span class="javascript"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p><img src= "https://cdn.jsdelivr.net/gh/Cai2w/cdn/img/loading.gif" data-lazy-src="D:\blog\source_posts\04-jQuery.assets\image-20211112134038607.png" alt="image-20211112134038607">    </p>
<h2 id="3-6-jQuery替换元素"><a href="#3-6-jQuery替换元素" class="headerlink" title="3,6 jQuery替换元素"></a>3,6 jQuery替换元素</h2><ul>
<li>**replaceWith() : **将指定元素替换成新的元素 , ==A.replaceWith(B) , 将B替换成A==</li>
<li>**replaceAll() : **把指定元素替换城新的元素 , ==A.replaceAll(B) , 将A替换成B==</li>
</ul>
<blockquote>
<p>代码实例</p>
</blockquote>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">&quot;box1&quot;</span>&gt;</span>我是box1<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">&quot;box2&quot;</span>&gt;</span>我是box2<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">type</span>=<span class="string">&quot;text/javascript&quot;</span>&gt;</span><span class="javascript"></span></span><br><span class="line"><span class="javascript">    <span class="comment">// 通过replaceWith将box1替换</span></span></span><br><span class="line"><span class="javascript">    <span class="keyword">var</span> $newDiv1 = $(<span class="string">&#x27;&lt;div&gt;我是通过replaceWith替换的元素&lt;/div&gt;&#x27;</span>);</span></span><br><span class="line"><span class="javascript">    $(<span class="string">&#x27;#box1&#x27;</span>).replaceWith($newDiv1);</span></span><br><span class="line"><span class="javascript"></span></span><br><span class="line"><span class="javascript">    <span class="comment">// 通过replaceAll将box2替换</span></span></span><br><span class="line"><span class="javascript">    <span class="keyword">var</span> $newDiv2 = $(<span class="string">&#x27;&lt;div&gt;我是通过replaceAll替换的元素&lt;/div&gt;&#x27;</span>);</span></span><br><span class="line"><span class="javascript">    $newDiv2.replaceAll($(<span class="string">&#x27;#box2&#x27;</span>));</span></span><br><span class="line"><span class="javascript"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>

<img src= "https://cdn.jsdelivr.net/gh/Cai2w/cdn/img/loading.gif" data-lazy-src="D:\blog\source\_posts\04-jQuery.assets\image-20211112140334821.png" alt="image-20211112140334821" style="zoom:50%;border: solid" />    



<h2 id="3-7-jQuery复制元素"><a href="#3-7-jQuery复制元素" class="headerlink" title="3,7 jQuery复制元素"></a>3,7 jQuery复制元素</h2><ul>
<li><p>**clone() : **用于赋值一个节点</p>
<p>参数 : true / false , 如果参数值为 true , 代表赋值事件处理</p>
</li>
</ul>
<blockquote>
<p>代码实例</p>
</blockquote>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">&quot;btn-box&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">button</span>&gt;</span>按我试试吧<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>脚本</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">$(<span class="string">&#x27;button&#x27;</span>).click(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    alert(<span class="string">&#x27;按钮被点击了&#x27;</span>);</span><br><span class="line">&#125;);</span><br><span class="line"><span class="comment">// 赋值#btn-box中第一个button标签,clone参数为true</span></span><br><span class="line"><span class="comment">// 表示会将事件一同复制过去</span></span><br><span class="line"><span class="keyword">var</span> $cloneBtn = $(<span class="string">&#x27;#btn-box button:first&#x27;</span>).clone(<span class="literal">true</span>);</span><br><span class="line"><span class="comment">// 在btn-box中复制这个按钮</span></span><br><span class="line">$(<span class="string">&#x27;#btn-box&#x27;</span>).append($cloneBtn);</span><br></pre></td></tr></table></figure>

<img src= "https://cdn.jsdelivr.net/gh/Cai2w/cdn/img/loading.gif" data-lazy-src="D:\blog\source\_posts\04-jQuery.assets\image-20211112141339141.png" alt="image-20211112141339141" style="zoom:50%;border: solid" />

<p>​    </p>
<h2 id="3-8-遍历-子级-同级-父级-元素"><a href="#3-8-遍历-子级-同级-父级-元素" class="headerlink" title="3,8 遍历 子级,同级,父级 元素"></a>3,8 遍历 子级,同级,父级 元素</h2><ul>
<li><p><strong>遍历子元素</strong></p>
<p>在 jQuery 中为了方便遍历元素的子元素 , 为我们封装了一个函数</p>
<p><strong>children()</strong></p>
</li>
<li><p><strong>遍历同级元素</strong></p>
<ul>
<li>**next() : **用于获取同级位置的下一个元素</li>
<li>**prev() : **用于获取同级位置的上一个元素</li>
<li>**siblings() : **获取所有的同级元素</li>
</ul>
</li>
<li><p><strong>遍历前辈元素</strong></p>
<ul>
<li>**parent() : **获取元素的父级元素</li>
<li>**parents() : **获取元素的祖先元素 (所有的,直到html)</li>
</ul>
</li>
</ul>
<blockquote>
<p>代码实例</p>
</blockquote>
<p>结构</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">ul</span> <span class="attr">id</span>=<span class="string">&quot;list&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span>&gt;</span>我是第1个li标签<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span>&gt;</span>我是第2个li标签<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span>&gt;</span>我是第3个li标签<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">ul</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p><strong>获取子元素</strong></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 通过 children()获取ul中所有li元素</span></span><br><span class="line"><span class="keyword">var</span> $child = $(<span class="string">&#x27;#list&#x27;</span>).children();</span><br><span class="line"><span class="built_in">console</span>.log($child);</span><br><span class="line"><span class="built_in">console</span>.log($child[<span class="number">0</span>]); <span class="comment">// 转换为js的DOM对象</span></span><br><span class="line"><span class="built_in">console</span>.log($(<span class="string">&#x27;#list&#x27;</span>).children()[<span class="number">0</span>]);</span><br></pre></td></tr></table></figure>



<p><strong>获取同级元素</strong></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 获取页面中第二个li标签</span></span><br><span class="line"><span class="keyword">var</span> $secondLi = $(<span class="string">&#x27;ul li:eq(1)&#x27;</span>);</span><br><span class="line"><span class="comment">// 通过 prev 获取第一个li元素</span></span><br><span class="line"><span class="built_in">console</span>.log($secondLi.prev()[<span class="number">0</span>]);</span><br><span class="line"><span class="comment">// 通过 next 获取第三个liu元素</span></span><br><span class="line"><span class="built_in">console</span>.log($secondLi.next()[<span class="number">0</span>]);</span><br><span class="line"><span class="comment">// 通过 siblings 获取同级的所有元素</span></span><br><span class="line"><span class="comment">// 并设置背景颜色为粉色</span></span><br><span class="line">$secondLi.siblings().css(<span class="string">&#x27;background-color&#x27;</span>,<span class="string">&#x27;pink&#x27;</span>);</span><br></pre></td></tr></table></figure>



<p><strong>获取祖先元素</strong></p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">ul</span> <span class="attr">id</span>=<span class="string">&quot;list&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span>&gt;</span>我是第1个li标签</span><br><span class="line">        <span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">&quot;son&quot;</span>&gt;</span>我是son<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span>&gt;</span>我是第2个li标签<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span>&gt;</span>我是第3个li标签<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">ul</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>脚本</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 获取id为son的div的父元素</span></span><br><span class="line"><span class="keyword">var</span> $father = $(<span class="string">&#x27;#son&#x27;</span>).parent();</span><br><span class="line"><span class="built_in">console</span>.log($father[<span class="number">0</span>]);<span class="comment">// 第一个li</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 获取id为son的div的祖先元素</span></span><br><span class="line"><span class="comment">// 获取所有的祖先元素 , 父级,父级的父级....</span></span><br><span class="line"><span class="keyword">var</span> $parents = $(<span class="string">&#x27;#son&#x27;</span>).parents();</span><br><span class="line"><span class="built_in">console</span>.log($parents);</span><br></pre></td></tr></table></figure>

<img src= "https://cdn.jsdelivr.net/gh/Cai2w/cdn/img/loading.gif" data-lazy-src="D:\blog\source\_posts\04-jQuery.assets\image-20211112145226637.png" alt="image-20211112145226637" style="zoom:50%;border: solid" />    





<ul>
<li><strong>案例 : 表格的各行换色</strong></li>
</ul>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">type</span>=<span class="string">&quot;text/javascript&quot;</span>&gt;</span><span class="javascript"></span></span><br><span class="line"><span class="javascript">    $(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">        <span class="comment">/* </span></span></span><br><span class="line"><span class="comment"><span class="javascript">			获取索引大于1的偶数的tr和索引大于1的奇数的tr</span></span></span><br><span class="line"><span class="comment"><span class="javascript">		*/</span></span></span><br><span class="line"><span class="javascript">        <span class="comment">// 链式编程</span></span></span><br><span class="line"><span class="javascript">        $(<span class="string">&#x27;tr:gt(1):even&#x27;</span>).css(<span class="string">&#x27;background-color&#x27;</span>,<span class="string">&#x27;pink&#x27;</span>)</span></span><br><span class="line"><span class="javascript">            .next().css(<span class="string">&#x27;background-color&#x27;</span>,<span class="string">&#x27;yellow&#x27;</span>);</span></span><br><span class="line"><span class="javascript">        <span class="comment">// 普通写法</span></span></span><br><span class="line"><span class="javascript">        $(<span class="string">&#x27;tr:gt(1):even&#x27;</span>).css(<span class="string">&#x27;background-color&#x27;</span>,<span class="string">&#x27;pink&#x27;</span>);</span></span><br><span class="line"><span class="javascript">        $(<span class="string">&#x27;tr:gt(1):odd&#x27;</span>).css(<span class="string">&#x27;background-color&#x27;</span>,<span class="string">&#x27;yellow&#x27;</span>);</span></span><br><span class="line"><span class="javascript">    &#125;);</span></span><br><span class="line"><span class="javascript"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p><img src= "https://cdn.jsdelivr.net/gh/Cai2w/cdn/img/loading.gif" data-lazy-src="D:\blog\source_posts\04-jQuery.assets\image-20211112153442479.png" alt="image-20211112153442479"></p>
<p><strong>全选功能</strong></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 获取全选的复选框</span></span><br><span class="line">$(<span class="string">&#x27;#checkAll&#x27;</span>).click(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="comment">// 将全选按钮的选中状态赋值给其他的复选按钮</span></span><br><span class="line">    $(<span class="string">&#x27;:checkbox:gt(0)&#x27;</span>).prop(<span class="string">&#x27;checked&#x27;</span>,$(<span class="built_in">this</span>).prop(<span class="string">&#x27;checked&#x27;</span>));</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>



<h1 id="4-jQuery-动画"><a href="#4-jQuery-动画" class="headerlink" title="4, jQuery 动画"></a>4, jQuery 动画</h1><h2 id="4-1-显示隐藏效果"><a href="#4-1-显示隐藏效果" class="headerlink" title="4,1 显示隐藏效果"></a>4,1 显示隐藏效果</h2><p>**语法 : **</p>
<ul>
<li><strong>show([speed] , [easing] , [fn])</strong> : 显示元素</li>
<li><strong>hide([speed] , [easing] , [fn])</strong> : 隐藏元素</li>
<li><strong>toggle([speed] , [easing] , [fn])</strong> : 切换元素的隐藏和显示效果</li>
</ul>
<p>**参数 : **</p>
<ul>
<li>**speed **速度 , 参数值字符串 ( ‘slow’ , ‘noraml’ , ‘fast’ ) , 或者是毫秒值</li>
<li><strong>easing</strong> 切换的效果 , swing摆动(默认) , 可用参数 linear (直线)</li>
<li><strong>fn</strong> 在动画完成时执行的函数 , 每个元素执行一次</li>
</ul>
<blockquote>
<p>代码实例</p>
</blockquote>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">button</span> <span class="attr">id</span>=<span class="string">&quot;btn1&quot;</span>&gt;</span>点击隐藏div<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">button</span> <span class="attr">id</span>=<span class="string">&quot;btn2&quot;</span>&gt;</span>点击显示div<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">button</span> <span class="attr">id</span>=<span class="string">&quot;btn3&quot;</span>&gt;</span>点击按钮切换显示与隐藏<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">&quot;box&quot;</span>&gt;</span></span><br><span class="line">    div的显示于隐藏</span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br></pre></td></tr></table></figure>

<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">style</span> <span class="attr">type</span>=<span class="string">&quot;text/css&quot;</span>&gt;</span><span class="css"></span></span><br><span class="line"><span class="css">    <span class="selector-id">#box</span> &#123;</span></span><br><span class="line"><span class="css">        <span class="attribute">width</span>: <span class="number">300px</span>;</span></span><br><span class="line"><span class="css">        <span class="attribute">height</span>: <span class="number">300px</span>;</span></span><br><span class="line"><span class="css">        <span class="attribute">background-color</span>: aqua;</span></span><br><span class="line"><span class="css">    &#125;</span></span><br><span class="line"><span class="css"></span><span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br></pre></td></tr></table></figure>

<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">type</span>=<span class="string">&quot;text/javascript&quot;</span>&gt;</span><span class="javascript"></span></span><br><span class="line"><span class="javascript">    $(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">        <span class="comment">// 隐藏div</span></span></span><br><span class="line"><span class="javascript">        $(<span class="string">&#x27;#btn1&#x27;</span>).click(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">            $(<span class="string">&#x27;#box&#x27;</span>).hide(<span class="number">1000</span>,<span class="string">&#x27;linear&#x27;</span>,<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span></span><br><span class="line"><span class="javascript">                alert(<span class="string">&#x27;我藏起来了~~~&#x27;</span>)</span></span><br><span class="line"><span class="javascript">            &#125;);</span></span><br><span class="line"><span class="javascript">        &#125;);</span></span><br><span class="line"><span class="javascript">        <span class="comment">// 显示div</span></span></span><br><span class="line"><span class="javascript">        $(<span class="string">&#x27;#btn2&#x27;</span>).click(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">            $(<span class="string">&#x27;#box&#x27;</span>).show(<span class="string">&#x27;fast&#x27;</span>,<span class="string">&#x27;linear&#x27;</span>,<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span></span><br><span class="line"><span class="javascript">                alert(<span class="string">&#x27;我出来了~~~&#x27;</span>)</span></span><br><span class="line"><span class="javascript">            &#125;);</span></span><br><span class="line"><span class="javascript">        &#125;);</span></span><br><span class="line"><span class="javascript">        <span class="comment">// 切换显示与隐藏</span></span></span><br><span class="line"><span class="javascript">        $(<span class="string">&#x27;#btn3&#x27;</span>).click(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">            $(<span class="string">&#x27;#box&#x27;</span>).toggle(<span class="number">1000</span>,<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span></span><br><span class="line"><span class="javascript">                alert(<span class="string">&#x27;666666&#x27;</span>)</span></span><br><span class="line"><span class="javascript">            &#125;);</span></span><br><span class="line"><span class="javascript">        &#125;);</span></span><br><span class="line"><span class="javascript">    &#125;);</span></span><br><span class="line"><span class="javascript"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>

<img src= "https://cdn.jsdelivr.net/gh/Cai2w/cdn/img/loading.gif" data-lazy-src="D:\blog\source\_posts\04-jQuery.assets\image-20211112162041888.png" alt="image-20211112162041888" style="zoom:50%;" />    



<h2 id="4-2-滑动效果"><a href="#4-2-滑动效果" class="headerlink" title="4,2 滑动效果"></a>4,2 滑动效果</h2><ul>
<li>**slideDown([speed] , [easing] , [fn]) : **向下滑动</li>
<li>**slideUp([speed] , [easing] , [fn]) : **向上滑动</li>
<li>**slideToggle([speed] , [easing] , [fn]) : **切换元素的上下滑动</li>
</ul>
<blockquote>
<p>代码实例</p>
</blockquote>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">button</span> <span class="attr">id</span>=<span class="string">&quot;btn1&quot;</span>&gt;</span>点击隐藏div<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">button</span> <span class="attr">id</span>=<span class="string">&quot;btn2&quot;</span>&gt;</span>点击显示div<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">button</span> <span class="attr">id</span>=<span class="string">&quot;btn3&quot;</span>&gt;</span>点击按钮切换显示与隐藏<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">&quot;box&quot;</span>&gt;</span></span><br><span class="line">    div的显示于隐藏</span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>样式</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">style</span> <span class="attr">type</span>=<span class="string">&quot;text/css&quot;</span>&gt;</span><span class="css"></span></span><br><span class="line"><span class="css">    <span class="selector-id">#box</span> &#123;</span></span><br><span class="line"><span class="css">        <span class="attribute">width</span>: <span class="number">300px</span>;</span></span><br><span class="line"><span class="css">        <span class="attribute">height</span>: <span class="number">300px</span>;</span></span><br><span class="line"><span class="css">        <span class="attribute">background-color</span>: aqua;</span></span><br><span class="line"><span class="css">    &#125;</span></span><br><span class="line"><span class="css"></span><span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>脚本</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">type</span>=<span class="string">&quot;text/javascript&quot;</span>&gt;</span><span class="javascript"></span></span><br><span class="line"><span class="javascript">    $(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">        <span class="comment">// 向上滑动</span></span></span><br><span class="line"><span class="javascript">        $(<span class="string">&#x27;#btn1&#x27;</span>).click(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">            $(<span class="string">&#x27;#box&#x27;</span>).slideUp(<span class="number">1000</span>,<span class="string">&#x27;linear&#x27;</span>,<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">                <span class="built_in">console</span>.log(<span class="string">&#x27;我收起来了~~~&#x27;</span>);</span></span><br><span class="line"><span class="javascript">            &#125;);</span></span><br><span class="line"><span class="javascript">        &#125;);</span></span><br><span class="line"><span class="javascript">        <span class="comment">// 向下滑动</span></span></span><br><span class="line"><span class="javascript">        $(<span class="string">&#x27;#btn2&#x27;</span>).click(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">            $(<span class="string">&#x27;#box&#x27;</span>).slideDown(<span class="number">1000</span>,<span class="string">&#x27;linear&#x27;</span>,<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">                <span class="built_in">console</span>.log(<span class="string">&#x27;我展开来了~~~&#x27;</span>);</span></span><br><span class="line"><span class="javascript">            &#125;);</span></span><br><span class="line"><span class="javascript">        &#125;);</span></span><br><span class="line"><span class="javascript">        <span class="comment">// 向上向下切换</span></span></span><br><span class="line"><span class="javascript">        $(<span class="string">&#x27;#btn3&#x27;</span>).click(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">            $(<span class="string">&#x27;#box&#x27;</span>).slideToggle(<span class="number">1000</span>,<span class="string">&#x27;linear&#x27;</span>,<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">                <span class="built_in">console</span>.log(<span class="string">&#x27;完成切换!!&#x27;</span>);</span></span><br><span class="line"><span class="javascript">            &#125;);</span></span><br><span class="line"><span class="javascript">        &#125;);</span></span><br><span class="line"><span class="javascript">    &#125;);</span></span><br><span class="line"><span class="javascript"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>



<h2 id="4-3-淡入-淡出效果"><a href="#4-3-淡入-淡出效果" class="headerlink" title="4,3 淡入,淡出效果"></a>4,3 淡入,淡出效果</h2><ul>
<li><p>**fadeIn([speed] , [easing] , [fn]) : **淡入效果</p>
</li>
<li><p>**fadeOut([speed] , [easing] , [fn]) : **淡出效果</p>
</li>
<li><p>**fadeToggle([speed] , [easing] , [fn]) : **淡入淡出效果切换</p>
</li>
<li><p>**fedaTo([speed] , o , [easing] , [fn]) : **设置元素的透明度</p>
<p>o : opacity 1 ~ 0 的数字 , 设置透明度</p>
</li>
</ul>
<blockquote>
<p>代码实例</p>
</blockquote>
<p>结构</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">button</span> <span class="attr">id</span>=<span class="string">&quot;btn1&quot;</span>&gt;</span>点击按钮实现淡出效果<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">button</span> <span class="attr">id</span>=<span class="string">&quot;btn2&quot;</span>&gt;</span>点击按钮实现淡入效果<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">button</span> <span class="attr">id</span>=<span class="string">&quot;btn3&quot;</span>&gt;</span>点击按钮实现淡入淡出切换<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">button</span> <span class="attr">id</span>=<span class="string">&quot;btn4&quot;</span>&gt;</span>点击按钮实现元素透明度变换<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">&quot;box&quot;</span> <span class="attr">style</span>=<span class="string">&quot;width: 300px;height: 300px; background-color: chocolate&quot;</span>&gt;</span></span><br><span class="line">    点击按钮切换当前和的显示效果</span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>脚本</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">type</span>=<span class="string">&quot;text/javascript&quot;</span>&gt;</span><span class="javascript"></span></span><br><span class="line"><span class="javascript">    $(<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span></span><br><span class="line"><span class="javascript">        <span class="comment">// 实现盒子的淡出效果</span></span></span><br><span class="line"><span class="javascript">        $(<span class="string">&#x27;#btn1&#x27;</span>).click(<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span></span><br><span class="line"><span class="javascript">            $(<span class="string">&#x27;#box&#x27;</span>).fadeOut(<span class="number">1000</span>,<span class="string">&#x27;linear&#x27;</span>,<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">                <span class="built_in">console</span>.log(<span class="string">&#x27;元素淡出页面~~&#x27;</span>);</span></span><br><span class="line"><span class="javascript">            &#125;)</span></span><br><span class="line"><span class="javascript">        &#125;);</span></span><br><span class="line"><span class="javascript">        <span class="comment">// 实现盒子的淡入效果</span></span></span><br><span class="line"><span class="javascript">        $(<span class="string">&#x27;#btn2&#x27;</span>).click(<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span></span><br><span class="line"><span class="javascript">            $(<span class="string">&#x27;#box&#x27;</span>).fadeIn(<span class="number">1000</span>,<span class="string">&#x27;linear&#x27;</span>,<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">                <span class="built_in">console</span>.log(<span class="string">&#x27;元素淡入页面~~&#x27;</span>);</span></span><br><span class="line"><span class="javascript">            &#125;)</span></span><br><span class="line"><span class="javascript">        &#125;);</span></span><br><span class="line"><span class="javascript">        <span class="comment">// 实现盒子的淡入淡出切换效果</span></span></span><br><span class="line"><span class="javascript">        $(<span class="string">&#x27;#btn3&#x27;</span>).click(<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span></span><br><span class="line"><span class="javascript">            $(<span class="string">&#x27;#box&#x27;</span>).fadeToggle(<span class="number">1000</span>,<span class="string">&#x27;linear&#x27;</span>,<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">                <span class="built_in">console</span>.log(<span class="string">&#x27;元素淡入淡出切换&#x27;</span>);</span></span><br><span class="line"><span class="javascript">            &#125;)</span></span><br><span class="line"><span class="javascript">        &#125;);</span></span><br><span class="line"><span class="javascript">        <span class="comment">// 实现盒子透明度的切换效果</span></span></span><br><span class="line"><span class="javascript">        $(<span class="string">&#x27;#btn4&#x27;</span>).click(<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span></span><br><span class="line"><span class="javascript">            <span class="comment">// .5代表小数 0.5</span></span></span><br><span class="line"><span class="javascript">            $(<span class="string">&#x27;#box&#x27;</span>).fadeTo(<span class="number">1000</span>,<span class="number">0</span>,<span class="string">&#x27;linear&#x27;</span>,<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">                <span class="built_in">console</span>.log(<span class="string">&#x27;元素切换至透明效果&#x27;</span>);</span></span><br><span class="line"><span class="javascript">            &#125;)</span></span><br><span class="line"><span class="javascript">        &#125;);</span></span><br><span class="line"><span class="javascript">    &#125;);</span></span><br><span class="line"><span class="javascript"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>





<ul>
<li><strong>案例 : 页面打开 , 广告从上慢慢滑下显示 , 停留10秒钟 , 过五秒从下向上慢慢滑动隐藏</strong></li>
</ul>
<p>结构</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">&quot;main&quot;</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">&quot;img&quot;</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">img</span> <span class="attr">src</span>=<span class="string">&quot;img/mipad.webp&quot;</span> <span class="attr">width</span>=<span class="string">&quot;100%&quot;</span> /&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">&quot;msg&quot;</span>&gt;</span></span><br><span class="line">            页面的正文内容</span><br><span class="line">        <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>样式</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">style</span> <span class="attr">type</span>=<span class="string">&quot;text/css&quot;</span>&gt;</span><span class="css"></span></span><br><span class="line"><span class="css">    <span class="selector-id">#main</span> &#123;</span></span><br><span class="line"><span class="css">        <span class="attribute">width</span>: <span class="number">800px</span>;</span></span><br><span class="line"><span class="css">        <span class="attribute">margin</span>: <span class="number">0px</span> auto;</span></span><br><span class="line"><span class="css">    &#125;</span></span><br><span class="line"><span class="css">    <span class="selector-id">#msg</span> &#123;</span></span><br><span class="line"><span class="css">        <span class="attribute">width</span>: <span class="number">100%</span>;</span></span><br><span class="line"><span class="css">        <span class="attribute">height</span>: <span class="number">300px</span>;</span></span><br><span class="line"><span class="css">        <span class="attribute">background-color</span>: burlywood;</span></span><br><span class="line"><span class="css">    &#125;</span></span><br><span class="line"><span class="css">    <span class="selector-id">#img</span> &#123;</span></span><br><span class="line"><span class="css">        <span class="attribute">display</span>: none;</span></span><br><span class="line"><span class="css">    &#125;</span></span><br><span class="line"><span class="css"></span><span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>脚本</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">type</span>=<span class="string">&quot;text/javascript&quot;</span>&gt;</span><span class="javascript"></span></span><br><span class="line"><span class="javascript">    <span class="comment">// 1,添加一个页面加载的事件</span></span></span><br><span class="line"><span class="javascript">    $(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">        <span class="comment">// 2,设置一个定时器,过5秒后执行</span></span></span><br><span class="line"><span class="javascript">        <span class="built_in">setTimeout</span>(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">            <span class="comment">// 3,获取盒子设置下滑显示的效果</span></span></span><br><span class="line"><span class="javascript">            $(<span class="string">&#x27;#img&#x27;</span>).slideDown(<span class="number">3000</span>);</span></span><br><span class="line"><span class="javascript">        &#125;,<span class="number">5000</span>);</span></span><br><span class="line"><span class="javascript">        <span class="comment">// 4,设置一个定时器,过5秒后执行</span></span></span><br><span class="line"><span class="javascript">        <span class="built_in">setTimeout</span>(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">            <span class="comment">// 5,获取盒子设置上滑显示的效果</span></span></span><br><span class="line"><span class="javascript">            $(<span class="string">&#x27;#img&#x27;</span>).slideUp(<span class="number">3000</span>);</span></span><br><span class="line"><span class="javascript">        &#125;,<span class="number">10000</span>);</span></span><br><span class="line"><span class="javascript">    &#125;);</span></span><br><span class="line"><span class="javascript"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>





<h1 id="5-jQuery遍历"><a href="#5-jQuery遍历" class="headerlink" title="5,jQuery遍历"></a>5,jQuery遍历</h1><p>jQuery对象本身就是一个数组 , 通过 jQuery选择器获得的满足选择器条件的元素对象的集合体 , 因此通常需要对 jQuery 对象进行遍历</p>
<h2 id="5-1-原始的遍历方式"><a href="#5-1-原始的遍历方式" class="headerlink" title="5,1 原始的遍历方式"></a>5,1 原始的遍历方式</h2><ul>
<li>for循环</li>
</ul>
<blockquote>
<p>代码实例</p>
</blockquote>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">ul</span> <span class="attr">id</span>=<span class="string">&quot;game&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span>&gt;</span>英雄联盟<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span>&gt;</span>贪玩蓝月<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span>&gt;</span>热血合击<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span>&gt;</span>使命召唤<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span>&gt;</span>侠盗飞车<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">ul</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>脚本</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">$(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="comment">// 获取无序列表</span></span><br><span class="line">    <span class="keyword">var</span> $lis = $(<span class="string">&#x27;li&#x27;</span>);</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; $lis.length; i++) &#123;</span><br><span class="line">        <span class="built_in">console</span>.log($lis[i]);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>

<img src= "https://cdn.jsdelivr.net/gh/Cai2w/cdn/img/loading.gif" data-lazy-src="D:\blog\source\_posts\04-jQuery.assets\image-20211115140523802.png" alt="image-20211115140523802" style="zoom:50%;" />    



<h2 id="5-2-jQuery对象方法遍历"><a href="#5-2-jQuery对象方法遍历" class="headerlink" title="5,2 jQuery对象方法遍历"></a>5,2 jQuery对象方法遍历</h2><ul>
<li><strong>语法</strong></li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">jQuery对象.each(<span class="function"><span class="keyword">function</span>(<span class="params">index,element</span>)</span>&#123;</span><br><span class="line">   <span class="comment">// index:元素在集合中的索引</span></span><br><span class="line">   <span class="comment">// element:集合中的每一个元素</span></span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>

<blockquote>
<p>代码实例</p>
</blockquote>
<p>结构</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">ul</span> <span class="attr">id</span>=<span class="string">&quot;game&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span>&gt;</span>英雄联盟<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span>&gt;</span>贪玩蓝月<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span>&gt;</span>热血合击<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span>&gt;</span>使命召唤<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span>&gt;</span>侠盗飞车<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">ul</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>脚本</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 2,jQuery对象each()方法遍历</span></span><br><span class="line">$lis.each(<span class="function"><span class="keyword">function</span>(<span class="params">index,element</span>)</span>&#123;</span><br><span class="line">    <span class="comment">// element是一个DOM对象</span></span><br><span class="line">    <span class="built_in">console</span>.log(element);</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>

<img src= "https://cdn.jsdelivr.net/gh/Cai2w/cdn/img/loading.gif" data-lazy-src="D:\blog\source\_posts\04-jQuery.assets\image-20211115141130516.png" alt="image-20211115141130516" style="zoom:67%;" />    



<h2 id="5-3-jQuery全局方法遍历"><a href="#5-3-jQuery全局方法遍历" class="headerlink" title="5,3 jQuery全局方法遍历"></a>5,3 jQuery全局方法遍历</h2><ul>
<li><strong>语法</strong></li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">$.each(jQuery对象,<span class="function"><span class="keyword">function</span>(<span class="params">index,element</span>)(<span class="params"></span>)</span>&#123;</span><br><span class="line">	<span class="comment">// index:元素在集合中的索引</span></span><br><span class="line">	<span class="comment">// element:集合中的每一个元素 </span></span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>

<blockquote>
<p>代码实例</p>
</blockquote>
<p>结构</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">ul</span> <span class="attr">id</span>=<span class="string">&quot;game&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span>&gt;</span>英雄联盟<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span>&gt;</span>贪玩蓝月<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span>&gt;</span>热血合击<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span>&gt;</span>使命召唤<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span>&gt;</span>侠盗飞车<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">ul</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>脚本</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">console</span>.log(<span class="string">&#x27;------jQuery全局方法遍历------&#x27;</span>);</span><br><span class="line">$.each($lis,<span class="function"><span class="keyword">function</span>(<span class="params">index,element</span>)</span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(index);</span><br><span class="line">    <span class="built_in">console</span>.log(element.innerHTML);</span><br><span class="line">    <span class="built_in">console</span>.log($(element).html());</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>

<img src= "https://cdn.jsdelivr.net/gh/Cai2w/cdn/img/loading.gif" data-lazy-src="D:\blog\source\_posts\04-jQuery.assets\image-20211115141816662.png" alt="image-20211115141816662" style="zoom:33%;border: solid" />    





<h2 id="5-4-jQuery3-0的特性-for-of-语句"><a href="#5-4-jQuery3-0的特性-for-of-语句" class="headerlink" title="5,4 jQuery3.0的特性 for of 语句"></a>5,4 jQuery3.0的特性 for of 语句</h2><ul>
<li><strong>语法</strong></li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span>(<span class="keyword">var</span> 变量 <span class="keyword">of</span> jQuery对象) &#123;</span><br><span class="line">	<span class="comment">// 变量:依次接收 jQuery 数组中的每一个元素</span></span><br><span class="line">    <span class="comment">// jQuery对象: 就是需要遍历的对象</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<blockquote>
<p>代码实例</p>
</blockquote>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">ul</span> <span class="attr">id</span>=<span class="string">&quot;game&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span>&gt;</span>英雄联盟<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span>&gt;</span>贪玩蓝月<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span>&gt;</span>热血合击<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span>&gt;</span>使命召唤<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span>&gt;</span>侠盗飞车<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">ul</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>脚本</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">console</span>.log(<span class="string">&#x27;-----jQuery3.0的for_of语法-----&#x27;</span>);</span><br><span class="line"><span class="keyword">for</span>(<span class="keyword">var</span> li <span class="keyword">of</span> $lis) &#123;</span><br><span class="line">    <span class="comment">// li:就是一个DOM对象</span></span><br><span class="line">    <span class="built_in">console</span>.log(li.innerHTML);</span><br><span class="line">    <span class="built_in">console</span>.log($(li).html());</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<img src= "https://cdn.jsdelivr.net/gh/Cai2w/cdn/img/loading.gif" data-lazy-src="D:\blog\source\_posts\04-jQuery.assets\image-20211115142721178.png" alt="image-20211115142721178" style="zoom:33%;border: solid" />    





<h1 id="6-jQuery事件"><a href="#6-jQuery事件" class="headerlink" title="6, jQuery事件"></a>6, jQuery事件</h1><h2 id="6-1-常用事件"><a href="#6-1-常用事件" class="headerlink" title="6,1 常用事件"></a>6,1 常用事件</h2><p>jQuery绑定事件 , 事件名字没有 on</p>
<table>
<thead>
<tr>
<th>事件名称</th>
<th>事件说明</th>
</tr>
</thead>
<tbody><tr>
<td>blur</td>
<td>事件源失去焦点</td>
</tr>
<tr>
<td>focus</td>
<td>事件源获取焦点</td>
</tr>
<tr>
<td>change</td>
<td>内容改变</td>
</tr>
<tr>
<td>click</td>
<td>单机事件源</td>
</tr>
<tr>
<td>keydown</td>
<td>键盘按键按下</td>
</tr>
<tr>
<td>keyup</td>
<td>键盘按键弹起</td>
</tr>
<tr>
<td>mouseover</td>
<td>鼠标移入</td>
</tr>
<tr>
<td>mouseout</td>
<td>鼠标移出</td>
</tr>
</tbody></table>
<h2 id="6-2-on事件绑定"><a href="#6-2-on事件绑定" class="headerlink" title="6,2 on事件绑定"></a>6,2 on事件绑定</h2><ul>
<li>**语法 **</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">jQuery对象.on(<span class="string">&#x27;事件名称&#x27;</span>,<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">   <span class="comment">// 回调函数的逻辑代码 </span></span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>

<blockquote>
<p>代码实例</p>
</blockquote>
<p>结构</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">&quot;button&quot;</span> <span class="attr">id</span>=<span class="string">&quot;btn&quot;</span> <span class="attr">value</span>=<span class="string">&quot;使用on绑定点击事件&quot;</span> /&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>脚本</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">type</span>=<span class="string">&quot;text/javascript&quot;</span>&gt;</span><span class="javascript"></span></span><br><span class="line"><span class="javascript">    $(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">        <span class="comment">// 方式一</span></span></span><br><span class="line"><span class="javascript">        <span class="comment">// $(&#x27;#btn&#x27;).click(function() &#123;</span></span></span><br><span class="line"><span class="javascript">        <span class="comment">// 	console.log(&#x27;事件函数绑定:按钮被点击了&#x27;);</span></span></span><br><span class="line"><span class="javascript">        <span class="comment">// &#125;);</span></span></span><br><span class="line"><span class="javascript"></span></span><br><span class="line"><span class="javascript">        <span class="comment">// 方式二:通过on函数绑定</span></span></span><br><span class="line"><span class="javascript">        $(<span class="string">&#x27;#btn&#x27;</span>).on(<span class="string">&#x27;click&#x27;</span>,<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">            <span class="built_in">console</span>.log(<span class="string">&#x27;on函数绑定:按钮被点击了&#x27;</span>);</span></span><br><span class="line"><span class="javascript">        &#125;);</span></span><br><span class="line"><span class="javascript">    &#125;);</span></span><br><span class="line"><span class="javascript"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>

<img src= "https://cdn.jsdelivr.net/gh/Cai2w/cdn/img/loading.gif" data-lazy-src="D:\blog\source\_posts\04-jQuery.assets\image-20211115145828488.png" alt="image-20211115145828488" style="zoom:50%;border: solid" />    



<ul>
<li><p><strong>案例 : 鼠标经过移出案例</strong></p>
<p><img src= "https://cdn.jsdelivr.net/gh/Cai2w/cdn/img/loading.gif" data-lazy-src="D:\blog\source_posts\04-jQuery.assets\image-20211115152208605.png" alt="image-20211115152208605">    </p>
</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">$(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="comment">// 获取页面中所有的tr,进行遍历</span></span><br><span class="line">    $(<span class="string">&#x27;tr&#x27;</span>).each(<span class="function"><span class="keyword">function</span>(<span class="params">i,e</span>)</span>&#123;</span><br><span class="line">        <span class="comment">// 给每个tr绑定一个鼠标经过的事件</span></span><br><span class="line">        $(e).on(<span class="string">&#x27;mouseover&#x27;</span>,<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">            $(<span class="built_in">this</span>).css(<span class="string">&#x27;background-color&#x27;</span>,<span class="string">&#x27;red&#x27;</span>)</span><br><span class="line">        &#125;);</span><br><span class="line">        $(e).on(<span class="string">&#x27;mouseout&#x27;</span>,<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">            $(<span class="built_in">this</span>).css(<span class="string">&#x27;background-color&#x27;</span>,<span class="string">&#x27;#eeeeee&#x27;</span>)</span><br><span class="line">        &#125;);</span><br><span class="line">    &#125;);</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>





<h2 id="6-3-off接触绑定"><a href="#6-3-off接触绑定" class="headerlink" title="6,3 off接触绑定"></a>6,3 off接触绑定</h2><ul>
<li><strong>语法</strong></li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">jQuery对象.off(事件名称);</span><br></pre></td></tr></table></figure>

<p><strong>注意 : 如果 off 中没有参数 , 可以接触该 jQuery 对象上所有的事件</strong></p>
<blockquote>
<p>代码实例</p>
</blockquote>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">button</span> <span class="attr">id</span>=<span class="string">&quot;btn1&quot;</span>&gt;</span>测试事件的绑定<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">button</span> <span class="attr">id</span>=<span class="string">&quot;btn2&quot;</span>&gt;</span>测试事件的解绑<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">type</span>=<span class="string">&quot;text/javascript&quot;</span>&gt;</span><span class="javascript"></span></span><br><span class="line"><span class="javascript">    <span class="comment">// 给btn1按钮绑定点击事件</span></span></span><br><span class="line"><span class="javascript">    $(<span class="string">&#x27;#btn1&#x27;</span>).on(<span class="string">&#x27;click&#x27;</span>,<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">        <span class="built_in">console</span>.log(<span class="string">&#x27;btn1按钮的点击事件被触发了&#x27;</span>);</span></span><br><span class="line"><span class="javascript">    &#125;);</span></span><br><span class="line"><span class="javascript"></span></span><br><span class="line"><span class="javascript">    <span class="comment">// 给btn2添加点击事件,将btn1的点击时间解绑</span></span></span><br><span class="line"><span class="javascript">    $(<span class="string">&#x27;#btn2&#x27;</span>).on(<span class="string">&#x27;click&#x27;</span>,<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span></span><br><span class="line"><span class="javascript">        $(<span class="string">&#x27;#btn1&#x27;</span>).off(<span class="string">&#x27;click&#x27;</span>);</span></span><br><span class="line"><span class="javascript">    &#125;);</span></span><br><span class="line"><span class="javascript"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p><img src= "https://cdn.jsdelivr.net/gh/Cai2w/cdn/img/loading.gif" data-lazy-src="D:\blog\source_posts\04-jQuery.assets\image-20211115154710184.png" alt="image-20211115154710184">    </p>
<h2 id="6-4-事件的切换"><a href="#6-4-事件的切换" class="headerlink" title="6,4 事件的切换"></a>6,4 事件的切换</h2><blockquote>
<p>代码实例</p>
</blockquote>
<p>结构</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">    <span class="comment">&lt;!-- 鼠标移入元素时背景变为红色,移出时变为绿色 --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">&quot;myDiv&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p><strong>原有写法</strong></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">$(<span class="string">&#x27;#myDiv&#x27;</span>).mouseover(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    $(<span class="built_in">this</span>).css(<span class="string">&#x27;background-color&#x27;</span>,<span class="string">&#x27;red&#x27;</span>);</span><br><span class="line">&#125;);</span><br><span class="line">$(<span class="string">&#x27;#myDiv&#x27;</span>).mouseout(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    $(<span class="built_in">this</span>).css(<span class="string">&#x27;background-color&#x27;</span>,<span class="string">&#x27;blue&#x27;</span>);</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>



<p><strong>链式编程</strong></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">$(<span class="string">&#x27;#myDiv&#x27;</span>).mouseover(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    $(<span class="built_in">this</span>).css(<span class="string">&#x27;background-color&#x27;</span>,<span class="string">&#x27;red&#x27;</span>);</span><br><span class="line">&#125;).mouseout(<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    $(<span class="built_in">this</span>).css(<span class="string">&#x27;background-color&#x27;</span>,<span class="string">&#x27;blue&#x27;</span>);</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>





<ul>
<li><strong>切换函数 : hover(over,out)</strong><ul>
<li>**over : **代表鼠标移入事件触发的函数</li>
<li><strong>out :</strong> 代表鼠标移出事件触发的函数</li>
</ul>
</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">$(<span class="string">&#x27;#myDiv&#x27;</span>).hover(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="comment">// 第一个匿名函数代表着鼠标移入事件时触发的函数</span></span><br><span class="line">    $(<span class="built_in">this</span>).css(<span class="string">&#x27;background-color&#x27;</span>,<span class="string">&#x27;red&#x27;</span>);</span><br><span class="line">&#125;,<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="comment">// 第二个匿名函数代表着鼠标移除事件时触发的函数</span></span><br><span class="line">    $(<span class="built_in">this</span>).css(<span class="string">&#x27;background-color&#x27;</span>,<span class="string">&#x27;blue&#x27;</span>);</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>























</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">lee</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="http://blog.cai2.wang/2021/11/20/Done/jQuery/">http://blog.cai2.wang/2021/11/20/Done/jQuery/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="http://blog.cai2.wang" target="_blank">李世龙</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/jQuery/">jQuery</a></div><div class="post_share"><div class="social-share" data-image="https://cdn.jsdelivr.net/gh/Cai2w/cdn/img/undraw_social_girl_562b.png" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/social-share.js/dist/js/social-share.min.js" defer></script></div></div><div class="post-reward"><div class="reward-button button--animated"><i class="fas fa-qrcode"></i> 打赏</div><div class="reward-main"><ul class="reward-all"><li class="reward-item"><a href="https://cdn.jsdelivr.net/gh/Cai2w/cdn/img/wechat.jpg" target="_blank"><img class="post-qr-code-img" src="https://cdn.jsdelivr.net/gh/Cai2w/cdn/img/wechat.jpg" alt="微信"/></a><div class="post-qr-code-desc">微信</div></li><li class="reward-item"><a href="https://cdn.jsdelivr.net/gh/Cai2w/cdn/img/alipay.jpg" target="_blank"><img class="post-qr-code-img" src="https://cdn.jsdelivr.net/gh/Cai2w/cdn/img/alipay.jpg" alt="支付宝"/></a><div class="post-qr-code-desc">支付宝</div></li></ul></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2021/12/01/Done/JavaScript/"><img class="prev-cover" src="https://cdn.jsdelivr.net/gh/Cai2w/cdn/img/undraw_Freelancer_re_irh4.png" onerror="onerror=null;src='https://cdn.jsdelivr.net/gh/Cai2w/cdn/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">javaScript</div></div></a></div><div class="next-post pull-right"><a href="/2021/11/19/Done/css3/"><img class="next-cover" src="https://cdn.jsdelivr.net/gh/Cai2w/cdn/img/undraw_cloud_hosting_aodd.png" onerror="onerror=null;src='https://cdn.jsdelivr.net/gh/Cai2w/cdn/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">css3</div></div></a></div></nav></div><div class="aside-content" id="aside-content"><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#1-jQuery-%E4%BB%8B%E7%BB%8D"><span class="toc-number">1.</span> <span class="toc-text">1, jQuery 介绍</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#1-1-jQuery%E6%A6%82%E8%BF%B0"><span class="toc-number">1.1.</span> <span class="toc-text">1,1 jQuery概述</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#1-2-jQuery%E6%A1%86%E6%9E%B6%E7%9A%84%E4%B8%8B%E8%BD%BD%E4%B8%8E%E5%BC%95%E7%94%A8"><span class="toc-number">1.2.</span> <span class="toc-text">1,2 jQuery框架的下载与引用</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#1-3-jQuery%E7%9A%84%E7%89%88%E6%9C%AC"><span class="toc-number">1.3.</span> <span class="toc-text">1,3 jQuery的版本</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#1-4-jQuery%E5%BC%95%E5%85%A5"><span class="toc-number">1.4.</span> <span class="toc-text">1,4 jQuery引入</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#1-5-jQuery%E9%A1%B5%E9%9D%A2%E5%8A%A0%E8%BD%BD%E4%BA%8B%E4%BB%B6%E5%92%8CJS%E9%A1%B5%E9%9D%A2%E5%8A%A0%E8%BD%BD%E4%BA%8B%E4%BB%B6%E7%9A%84%E5%8C%BA%E5%88%AB"><span class="toc-number">1.5.</span> <span class="toc-text">1,5 jQuery页面加载事件和JS页面加载事件的区别</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#1-6-jQuery%E5%AF%B9%E8%B1%A1%E5%92%8Cjs%E5%AF%B9%E8%B1%A1%E4%B9%8B%E9%97%B4%E7%9A%84%E8%BD%AC%E6%8D%A2"><span class="toc-number">1.6.</span> <span class="toc-text">1,6 jQuery对象和js对象之间的转换</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#2-jQuery-%E9%80%89%E6%8B%A9%E5%99%A8"><span class="toc-number">2.</span> <span class="toc-text">2, jQuery 选择器</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#2-1-%E5%9F%BA%E6%9C%AC%E9%80%89%E6%8B%A9%E5%99%A8"><span class="toc-number">2.1.</span> <span class="toc-text">2,1 基本选择器</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-2-%E5%B1%82%E7%BA%A7%E9%80%89%E6%8B%A9%E5%99%A8"><span class="toc-number">2.2.</span> <span class="toc-text">2,2 层级选择器</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-3-%E5%B1%9E%E6%80%A7%E9%80%89%E6%8B%A9%E5%99%A8"><span class="toc-number">2.3.</span> <span class="toc-text">2,3 属性选择器</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-4-%E5%9F%BA%E6%9C%AC%E8%BF%87%E6%BB%A4%E9%80%89%E6%8B%A9%E5%99%A8"><span class="toc-number">2.4.</span> <span class="toc-text">2,4 基本过滤选择器</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-5-%E8%A1%A8%E5%8D%95%E5%B1%9E%E6%80%A7%E9%80%89%E6%8B%A9%E5%99%A8"><span class="toc-number">2.5.</span> <span class="toc-text">2,5 表单属性选择器</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#3-jQuery%E7%9A%84DOM%E6%93%8D%E4%BD%9C"><span class="toc-number">3.</span> <span class="toc-text">3, jQuery的DOM操作</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#3-1-jQuery%E6%93%8D%E4%BD%9C%E5%85%83%E7%B4%A0%E5%86%85%E5%AE%B9"><span class="toc-number">3.1.</span> <span class="toc-text">3,1 jQuery操作元素内容</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-2-jQuery%E6%93%8D%E4%BD%9C%E5%85%83%E7%B4%A0%E5%B1%9E%E6%80%A7"><span class="toc-number">3.2.</span> <span class="toc-text">3,2 jQuery操作元素属性</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-3-jQuery%E6%93%8D%E4%BD%9C%E6%A0%B7%E5%BC%8F"><span class="toc-number">3.3.</span> <span class="toc-text">3,3 jQuery操作样式</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-4-jQuery%E5%88%9B%E5%BB%BA%E5%92%8C%E6%8F%92%E5%85%A5%E5%AF%B9%E8%B1%A1"><span class="toc-number">3.4.</span> <span class="toc-text">3,4 jQuery创建和插入对象</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-5-jQuery%E5%88%A0%E9%99%A4%E5%AF%B9%E8%B1%A1"><span class="toc-number">3.5.</span> <span class="toc-text">3,5 jQuery删除对象</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-6-jQuery%E6%9B%BF%E6%8D%A2%E5%85%83%E7%B4%A0"><span class="toc-number">3.6.</span> <span class="toc-text">3,6 jQuery替换元素</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-7-jQuery%E5%A4%8D%E5%88%B6%E5%85%83%E7%B4%A0"><span class="toc-number">3.7.</span> <span class="toc-text">3,7 jQuery复制元素</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-8-%E9%81%8D%E5%8E%86-%E5%AD%90%E7%BA%A7-%E5%90%8C%E7%BA%A7-%E7%88%B6%E7%BA%A7-%E5%85%83%E7%B4%A0"><span class="toc-number">3.8.</span> <span class="toc-text">3,8 遍历 子级,同级,父级 元素</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#4-jQuery-%E5%8A%A8%E7%94%BB"><span class="toc-number">4.</span> <span class="toc-text">4, jQuery 动画</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#4-1-%E6%98%BE%E7%A4%BA%E9%9A%90%E8%97%8F%E6%95%88%E6%9E%9C"><span class="toc-number">4.1.</span> <span class="toc-text">4,1 显示隐藏效果</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-2-%E6%BB%91%E5%8A%A8%E6%95%88%E6%9E%9C"><span class="toc-number">4.2.</span> <span class="toc-text">4,2 滑动效果</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-3-%E6%B7%A1%E5%85%A5-%E6%B7%A1%E5%87%BA%E6%95%88%E6%9E%9C"><span class="toc-number">4.3.</span> <span class="toc-text">4,3 淡入,淡出效果</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#5-jQuery%E9%81%8D%E5%8E%86"><span class="toc-number">5.</span> <span class="toc-text">5,jQuery遍历</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#5-1-%E5%8E%9F%E5%A7%8B%E7%9A%84%E9%81%8D%E5%8E%86%E6%96%B9%E5%BC%8F"><span class="toc-number">5.1.</span> <span class="toc-text">5,1 原始的遍历方式</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#5-2-jQuery%E5%AF%B9%E8%B1%A1%E6%96%B9%E6%B3%95%E9%81%8D%E5%8E%86"><span class="toc-number">5.2.</span> <span class="toc-text">5,2 jQuery对象方法遍历</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#5-3-jQuery%E5%85%A8%E5%B1%80%E6%96%B9%E6%B3%95%E9%81%8D%E5%8E%86"><span class="toc-number">5.3.</span> <span class="toc-text">5,3 jQuery全局方法遍历</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#5-4-jQuery3-0%E7%9A%84%E7%89%B9%E6%80%A7-for-of-%E8%AF%AD%E5%8F%A5"><span class="toc-number">5.4.</span> <span class="toc-text">5,4 jQuery3.0的特性 for of 语句</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#6-jQuery%E4%BA%8B%E4%BB%B6"><span class="toc-number">6.</span> <span class="toc-text">6, jQuery事件</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#6-1-%E5%B8%B8%E7%94%A8%E4%BA%8B%E4%BB%B6"><span class="toc-number">6.1.</span> <span class="toc-text">6,1 常用事件</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#6-2-on%E4%BA%8B%E4%BB%B6%E7%BB%91%E5%AE%9A"><span class="toc-number">6.2.</span> <span class="toc-text">6,2 on事件绑定</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#6-3-off%E6%8E%A5%E8%A7%A6%E7%BB%91%E5%AE%9A"><span class="toc-number">6.3.</span> <span class="toc-text">6,3 off接触绑定</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#6-4-%E4%BA%8B%E4%BB%B6%E7%9A%84%E5%88%87%E6%8D%A2"><span class="toc-number">6.4.</span> <span class="toc-text">6,4 事件的切换</span></a></li></ol></li></ol></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2021 - 2022 By lee</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div><div class="footer_custom_text">这是footer</div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="font-plus" type="button" title="放大字体"><i class="fas fa-plus"></i></button><button id="font-minus" type="button" title="缩小字体"><i class="fas fa-minus"></i></button><button id="translateLink" type="button" title="简繁转换">繁</button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><div class="search-dialog__title" id="local-search-title">本地搜索</div><div id="local-input-panel"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div></div><hr/><div id="local-search-results"></div><span class="search-close-button"><i class="fas fa-times"></i></span></div><div id="search-mask"></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="/js/tw_cn.js"></script><script src="https://cdn.jsdelivr.net/npm/instant.page/instantpage.min.js" type="module"></script><script src="https://cdn.jsdelivr.net/npm/vanilla-lazyload/dist/lazyload.iife.min.js"></script><script src="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.js"></script><script>function panguFn () {
  if (typeof pangu === 'object') pangu.autoSpacingPage()
  else {
    getScript('https://cdn.jsdelivr.net/npm/pangu/dist/browser/pangu.min.js')
      .then(() => {
        pangu.autoSpacingPage()
      })
  }
}

function panguInit () {
  if (true){
    GLOBAL_CONFIG_SITE.isPost && panguFn()
  } else {
    panguFn()
  }
}

document.addEventListener('DOMContentLoaded', panguInit)</script><script src="/js/search/local-search.js"></script><script>var preloader = {
  endLoading: () => {
    document.body.style.overflow = 'auto';
    document.getElementById('loading-box').classList.add("loaded")
  },
  initLoading: () => {
    document.body.style.overflow = '';
    document.getElementById('loading-box').classList.remove("loaded")

  }
}
window.addEventListener('load',preloader.endLoading())</script><div class="js-pjax"><script>if (document.getElementsByClassName('mermaid').length) {
  if (window.mermaidJsLoad) mermaid.init()
  else {
    getScript('https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.min.js').then(() => {
      window.mermaidJsLoad = true
      mermaid.initialize({
        theme: 'default',
      })
      true && mermaid.init()
    })
  }
}</script></div><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/activate-power-mode.min.js"></script><script>POWERMODE.colorful = true;
POWERMODE.shake = false;
POWERMODE.mobile = false;
document.body.addEventListener('input', POWERMODE);
</script><script id="click-heart" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/click-heart.min.js" async="async" mobile="false"></script><script src="https://cdn.jsdelivr.net/npm/pjax/pjax.min.js"></script><script>let pjaxSelectors = [
  'title',
  '#config-diff',
  '#body-wrap',
  '#rightside-config-hide',
  '#rightside-config-show',
  '.js-pjax'
]

if (false) {
  pjaxSelectors.unshift('meta[property="og:image"]', 'meta[property="og:title"]', 'meta[property="og:url"]')
}

var pjax = new Pjax({
  elements: 'a:not([target="_blank"])',
  selectors: pjaxSelectors,
  cacheBust: false,
  analytics: false,
  scrollRestoration: false
})

document.addEventListener('pjax:send', function () {

  // removeEventListener scroll 
  window.removeEventListener('scroll', window.tocScrollFn)
  window.removeEventListener('scroll', scrollCollect)

  typeof preloader === 'object' && preloader.initLoading()
  
  if (window.aplayers) {
    for (let i = 0; i < window.aplayers.length; i++) {
      if (!window.aplayers[i].options.fixed) {
        window.aplayers[i].destroy()
      }
    }
  }

  typeof typed === 'object' && typed.destroy()

  //reset readmode
  const $bodyClassList = document.body.classList
  $bodyClassList.contains('read-mode') && $bodyClassList.remove('read-mode')

})

document.addEventListener('pjax:complete', function () {
  window.refreshFn()

  document.querySelectorAll('script[data-pjax]').forEach(item => {
    const newScript = document.createElement('script')
    const content = item.text || item.textContent || item.innerHTML || ""
    Array.from(item.attributes).forEach(attr => newScript.setAttribute(attr.name, attr.value))
    newScript.appendChild(document.createTextNode(content))
    item.parentNode.replaceChild(newScript, item)
  })

  GLOBAL_CONFIG.islazyload && window.lazyLoadInstance.update()

  typeof chatBtnFn === 'function' && chatBtnFn()
  typeof panguInit === 'function' && panguInit()

  // google analytics
  typeof gtag === 'function' && gtag('config', '', {'page_path': window.location.pathname});

  // baidu analytics
  typeof _hmt === 'object' && _hmt.push(['_trackPageview',window.location.pathname]);

  typeof loadMeting === 'function' && document.getElementsByClassName('aplayer').length && loadMeting()

  // Analytics
  if (false) {
    MtaH5.pgv()
  }

  // prismjs
  typeof Prism === 'object' && Prism.highlightAll()

  typeof preloader === 'object' && preloader.endLoading()
})

document.addEventListener('pjax:error', (e) => {
  if (e.request.status === 404) {
    pjax.loadUrl('/404.html')
  }
})</script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>